Logo

Programming-Idioms

History of Idiom 31 > diff from v39 to v40

Edit summary for version 40 by anon20171221:
New Clojure implementation by user [anon20171221]

Version 39

2017-06-14, 15:08:13

Version 40

2017-12-21, 02:59:25

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
(defn f [i]
  (loop [i i acc 1N]
    (if (zero? i)
      acc
      (recur (dec i) (* i acc)))))
Comments bubble
This supports arbitrary-precision.
Replace 1N (clojure.lang.BigInt) with 1 (java.lang.Long) if you don't need to calculate anything above _(f 20) .
Demo URL
https://repl.it/repls/SerpentineFunnyYnambu