Search code examples
eclnewlisp

About factorial in newlisp


I run facorial function using ECL and newlisp.

ECL:

>(defun fac (n) (if (= n 1) 1 (* n (fac (- n 1)))))
>(fac 20)
22432902008176640000
>(fac 30)
2265252859812191058636308480000000
>(fac 40)
815915283247897734345611269596115894272000000000
...

newlisp

>(define (fac n) (if (= n 1) 1 (* n (fac (- n 1)))))
>(fac 20)
22432902008176640000
>(fac 30)
-8764578968847253504

Why does newlisp return different result with ecl ?


Solution

  • newLISP is designed to be small (~260K), and one of the things you won't get by default is support for really big integers, and the necessary automatic conversion between ordinary-sized and big integers. If you regularly need integers such as 1000! in your work, then you've probably chosen the wrong language. However, if you want to use newLISP for other reasons, and really really need to calculate 100!, then install the GMP library, and write your code as follows:

    (load "gmp.lsp")
    (define (factorial-gmp num)
     (if (= num 0)
      "1"
      (GMP:* (string num) (factorial-gmp (- num 1)))))