I wanted to compare how to do "sum of digits" in Scala and Pico Lisp, and
to my surprise my Pico Lisp version was much slower than my Scala version.
Well, Scala is a compiled language, but I hadn't anticipated that big a
difference. My test input number was a rather big one: (2^100 + 3^200)^300
(≈1.885 x 10^28627)

My Scala code was like this:
def sumOfDigits(n: BigInt): BigInt = n.toString.map{ _.asDigit
}.foldLeft(0){ _+_ }
val big = (2:BigInt).pow(100)+(3:BigInt).pow(200)
-> 128458

My Pico Lisp code was like this:
(de sumOfDigits (N)
        (apply + (mapcar format (chop (format N)))) )
(setq Big (+ (** 2 100) (** 3 200)))
(sumOfDigits (** Big 300))
-> 128458

Could I have written the Pico Lisp code differently to make it faster?


UNSUBSCRIBE: mailto:picol...@software-lab.de?subject=unsubscribe

Reply via email to