I have tried to implement naive calculation of the factorial in Lisp.
(defun factorial (n)
(if (equal n 1)
1
(* n (factorial (- n 1)))))
The code works for small numbers (< 10) as one would expect. However, I have been very surprised it also works for much higher numbers (e.g 1000) and the result is calculated almost instantly.
On the other hand, in C++ the following code retrieves 0 for factorial(1000).
long long unsigned factorial(int n)
{
if(n == 1) return 1;
return n * factorial(n-1);
}
Why is the calculation in Lisp so fast and how is the number stored in memory?