jeudi 27 septembre 2012

Strange number 123456

To exclude I/O from this post I've modified code. So now it not only calculates factorials but also sums their digits and at last sums the sums of digits and as a result there's on one I/O operation. Lisp as usual behaving quite good. And a result of this code: is
Evaluation took:
  7.762 seconds of real time
  7.565354 seconds of total run time (7.363660 user, 0.201694 system)
  [ Run times consist of 0.399 seconds GC time, and 7.167 seconds non-GC time. ]
  97.46% CPU
  17,812,436,719 processor cycles
  177 page faults
  15,624,347,568 bytes consed
  
2652733
Almost similar results I've got from Go program:
Elapsed time: 4.509443s
2652733
But strangely Clojure, Haskell and Julia have got some very strange behavior. Till 12345! everything seems to be ok (though for Clojure it takes almost 1.5 sec to calculate while Lisp done it in 0.091 seconds of real time). But when we get 123456! Clojure needs almost 1 minute, Haskell and Julia just plainly refused to calculate. Even in case of Julia it was not so plainly cause it decided to eat almost all memory.

Euler #125

The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 62 + 72 + 82 + 92 + 102 + 112 + 122...

dimanche 16 septembre 2012

What's wrong with the code?

Decided to take naive implementation of factorial in Racket and Lisp:
and

And even after
raco exe test.rkt
timing is as follows:
raco exe test.rkt
time ./test

real 0m0.697s
user 0m0.581s
sys 0m0.092s
time sbcl --script test.lisp

real 0m0.139s
user 0m0.085s
sys 0m0.049s
Maybe I/O in Racket is rather slow?