Almkvist-Giullera formula for pi: Difference between revisions

J version
(J version)
Line 670:
3.1415926535897932384626433832795028841971693993751058209749445923078164</pre>
 
=={{header|J}}==
This solution just has it hard-coded that 53 iterations is necessary for 70 decimals. It would be possible to write a loop with a test, though in practice it would also be acceptable to just experiment to find the number of iterations.
 
sqrt is noticeably slow, bring execution time to over 1 second. I'm not sure if it's because it's coded imperatively using traditional loops vs. J point-free style, or if it's due to the fact that the numbers are very large. I suspect the latter since it only takes 4 iterations of Heron's method to get the square root.
<lang J>
numerator =: monad define "0
(3 * (! x: y)^6) %~ 32 * (!6x*y) * (y*(126 + 532*y)) + 9x
)
 
term =: numerator % 10x ^ 3 + 6&*
 
echo 'The first 10 numerators are:'
echo 10 1 $ numerator i.10
 
echo ''
echo 'The sum of the first 10 terms pi^-2) is ', 0j15 ": +/ term i.10
 
heron =: [: -: ] + %
 
sqrt =: dyad define NB. usage: x0 tolerance sqrt x
NB. e.g.: (1, %10^100x) sqrt 2 -> √2 to 100 decimals as a ratio p/q
x0 =. }: x
eps =. }. x
x1 =. y heron x0
while. (| x1 - x0) > eps do.
x2 =. y heron x1
x0 =. x1
x1 =. x2
end.
x1
)
 
pi70 =. (355r113, %10^70x) sqrt % +/ term i.53
echo ''
echo 'pi to 70 decimals: ', 0j70 ": pi70
exit ''
</lang>
{{Out}}
<pre>
The first 10 numerators are:
96
5122560
190722470400
7574824857600000
312546150372456000000
13207874703225491420651520
567273919793089083292259942400
24650600248172987140112763715584000
1080657854354639453670407474439566400000
47701779391594966287470570490839978880000000
 
The sum of the first 10 terms pi^-2) is 0.101321183642336
 
pi to 70 decimals: 3.1415926535897932384626433832795028841971693993751058209749445923078164
</pre>
=={{header|JavaScript}}==
{{trans|Common Lisp}}
357

edits