Comment #0 by bearophile_hugs — 2011-05-15T09:56:05Z
This single-line Haskell code runs in about 0.04 seconds compiled with GHC -O3:
main = print $ (5^4^3^2) `mod` 10
This D2 code runs in about 0.09 seconds (DMD 2.053) showing that this computation is fast enough in D (GHC used GNU multiprecision):
import std.stdio, std.bigint;
void main() {
writeln((BigInt(5) ^^ 4 ^^ 3 ^^ 2) % 10);
}
This Haskell program runs in about 0.24 seconds (GHC -O3), and prints pieces of the number:
y = show ( 5^4^3^2 )
l = length y
main = do
putStrLn ("5**4**3**2 = " ++ take 20 y ++ "..." ++ drop (l-20) y ++ " and has " ++ show l ++ " digits")
A similar D2 program takes about 3.38 seconds, so I think the BigInt->string conversion is significantly slower:
import std.stdio, std.bigint;
void main() {
auto s = toDecimalString(BigInt(5) ^^ 4 ^^ 3 ^^ 2);
writefln("5^4^3^2 = %s..%s (%d digits)", s[0..20], s[$-20..$], s.length);
}
Some info about a proposal to speed up Python division and int->str conversions:
http://fredrik-j.blogspot.com/2008/07/making-division-in-python-faster.htmlhttp://bugs.python.org/issue3451
Comment #1 by robert.schadek — 2024-12-01T16:14:08Z