
On Wed, 25 Sep 2019, anaaugusto2012@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.

On Thu, 26 Sep 2019, Henning Thielemann wrote:
On Wed, 25 Sep 2019, anaaugusto2012@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.
Hm, computing with Integer gives a different result. Computing with 25 decimal places gives me: 927436437019 for the first 12 digits and this does not change anymore when I increase precision.
participants (2)
-
anaaugusto2012@bol.com.br
-
Henning Thielemann