言語の速度比較なんていくらでもあるけど。
四度実行して、maxとminを除いて2つを残す。 Haskellはghcで最適化オプションなし。
-- test.hs
fact 0 = 1
fact n = n * fact (n-1)
main = getLine >>= print.fact.read
{-
$ time ./test < input
815915283247897734345611269596115894272000000000
real 0m0.005s
user 0m0.000s
sys 0m0.004s
$ time ./test < input
815915283247897734345611269596115894272000000000
real 0m0.006s
user 0m0.000s
sys 0m0.000s
-}
;; test.scm
(define (fact n)
(if (zero? n) 1
(* n (fact (- n 1)))))
(print (fact (read)))
#|
$ time gosh test.scm < input
815915283247897734345611269596115894272000000000
real 0m0.029s
user 0m0.012s
sys 0m0.012s
$ time gosh test.scm < input
815915283247897734345611269596115894272000000000
real 0m0.029s
user 0m0.016s
sys 0m0.004s
|#
// test.js (多倍長演算の一部を実装してるので長い)
function mult(n, bi) {
var base = 19;
bi = bi.map(function(s){return s*n});
for (var i=0;i<bi.length;++i) {
var s = bi[i] + "";
if (s.length > base) {
var len = s.length;
bi[i] = +s.slice(len-base, len);
if ((i+1) in bi)
bi[i+1] += +s.slice(0, len-base);
else
bi[i+1] = +s.slice(0, len-base);
}
}
return bi;
}
function show(bi) {
var str = "", len = bi.length;
for (var i=0;i<len;++i)
str += bi[len-i-1];
return str;
}
function fact(n){
return n==0 ? [1] : mult(n, fact(n-1));
}
console.log(show(fact(+require("fs").readFileSync("/dev/stdin","utf-8"))))
time node test.js < input
815915283247897700418722000006115894272000000000
real 0m0.168s
user 0m0.072s
sys 0m0.020s
time node test.js < input
815915283247897700418722000006115894272000000000
real 0m0.169s
user 0m0.072s
sys 0m0.020s
ま、こんなもんよね