Logo

Programming-Idioms

History of Idiom 31 > diff from v17 to v18

Edit summary for version 18 by :

Version 17

2015-09-09, 09:01:18

Version 18

2015-09-09, 09:02:00

Idiom #31 Recursive factorial (simple)

Create recursive function f which returns the factorial of non-negative integer i, calculated from f(i-1)

Idiom #31 Recursive factorial (simple)

Create recursive function f which returns the factorial of non-negative integer i, calculated from f(i-1)

Code
f = Hash.new { |hash, i| hash[i] = i * hash[i -1] }
f[0] = 1
f[23] # => 25852016738884976640000
Code
f = Hash.new { |hash, i| hash[i] = i * hash[i -1] }
f[0] = 1
f[23] # => 25852016738884976640000
Comments bubble
Note that f is not a function but plain old `Hash` used as a cache for performance.
Comments bubble
Note that f is not a function but plain old Hash used as a cache for performance.