[web-devel] haskell memory allocation
Henning Thielemann
lemming at henning-thielemann.de
Wed Sep 25 23:06:58 UTC 2019
On Wed, 25 Sep 2019, anaaugusto2012 at bol.com.br wrote:
> I want to allocate more memory because my teacher challenged me to show
> him the first 12 and last 12 digits of the expression 2 ^ 17179869184
Maybe better suited for haskell-cafe?
I would be pretty waste of memory to compute the power completely.
You can get the first 12 digits using approximate multiplications and you
can get the last 12 digits using modulo arithmetic.
It holds 17179869184 = 2^34.
Prelude> mapM_ print $ take 35 $ iterate (\x -> let y = x*x in if y>=10 then y/10 else y) 2
2.0
4.0
1.6
2.5600000000000005
...
9.274368853264574
That is, the first 12 digits should be 927436885326. Hopefully,
computation was precise enough. If not, you might retry more precise
computations using Integer.
Prelude> mapM_ print $ take 35 $ iterate (\x -> mod (x*x) (10^12)) (2::Integer)
2
4
16
256
...
266397474816
The last 12 digits are 266397474816.
More information about the web-devel
mailing list