Posted By

darkphotn on 01/13/08


Tagged

number Large LISP factorial


Versions (?)

Large Factorials in LISP


 / Published in: Lisp
 

Large factorials are easy to do in LISP, although a number above (2000!) may crash it. I designed this because of an online post from a mathematician talking about the number 1000! . Code like this is difficult to write in C-like languages, but a snap in LISP, as the following code demonstrates.

  1. (defun factorial(x)
  2. (cond ((zerop x) 1)
  3. ('T (* x (factorial (1- x)))) ))
  4.  
  5. (factorial 1000)

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jcubic on August 25, 2010

Recursive function use stack. this is stack-less version of factorial function which should work for very large factorials (like 10000 and more) which use tail recursion (which is replace to iteration by compiler)

(defun ! (n &optional (product 1))
(if (zerop n)
product
(! (1- n) (* product n))))

You need to login to post a comment.