Skip to content

Instantly share code, notes, and snippets.

@raek
Created June 17, 2010 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raek/442269 to your computer and use it in GitHub Desktop.
Save raek/442269 to your computer and use it in GitHub Desktop.
(defn fac [n]
(loop [n n, acc 1]
(if (zero? n)
acc
(recur (dec n) (* acc n)))))
(fac 20)
; => 2432902008176640000
(fac 25)
; => java.lang.ArithmeticException: integer overflow
(fac 20N)
; => 2432902008176640000
(fac 25N)
; => 7034535277573963776
(fac 26N)
; => -1569523520172457984
@richhickey
Copy link

(defn fac [n](loop [n n, acc %28num 1%29]
%28if %28zero? n%29
acc
%28recur %28dec n%29 %28* acc n%29%29%29))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment