Jump to content

Almkvist-Giullera formula for pi: Difference between revisions

m (Minor edit to C++ code)
Line 870:
 
Pi after 52 iterations: 3.1415926535897932384626433832795028841971693993751058209749445923078164</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with gojq, the Go implementation of jq'''
 
This entry uses the "rational" module, which can be found at [[Arithmetic/Rational#jq]].
 
'''Preliminaries'''
<lang jq># A reminder to include the "rational" module:
# include "rational";
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
def factorial:
if . < 2 then 1
else reduce range(2;.+1) as $i (1; .*$i)
end; </lang>
'''Almkvist-Giullera Formula'''
<lang jq>
def almkvistGiullera(print):
. as $n
| ((6*$n) | factorial * 32) as $t1
| (532*$n*$n + 126*$n + 9) as $t2
| (($n | factorial | power(6))*3) as $t3
| ($t1 * $t2 / $t3) as $ip
| ( 6*$n + 3) as $pw
| r($ip; 10 | power($pw)) as $tm
| if print
then "\($n|lpad(2)) \($ip|lpad(44)) \(-$pw|lpad(3)), \($tm|r_to_decimal(100))"
else $tm
end; </lang>
'''The Tasks'''
<lang jq>
def task1:
"N Integer Portion Pow Nth Term",
("-" * 89),
(range(0;10) | almkvistGiullera(true)) ;
 
def task2($precision):
r(1; 10 | power($precision)) as $p
| {sum: r(0;1), prev: r(0;1), n: 0 }
| until(.stop;
.sum = radd(.sum; .n | almkvistGiullera(false))
| if rminus(.sum; .prev) | rabs | rlessthan($p)
then .stop = true
else .prev = .sum
| .n += 1
end)
| .sum | rinv
| rsqrt($precision)
| "\nPi to \($precision) decimal places is:",
"\(r_to_decimal($precision))" ;
 
task1,
""
task2(70)</lang>
{{out}}
<pre>
N Integer Portion Pow Nth Term
-----------------------------------------------------------------------------------------
0 96 -3, 0.096
1 5122560 -9, 0.00512256
2 190722470400 -15, 0.0001907224704
3 7574824857600000 -21, 0.0000075748248576
4 312546150372456000000 -27, 0.000000312546150372456
5 13207874703225491420651520 -33, 0.00000001320787470322549142065152
6 567273919793089083292259942400 -39, 0.0000000005672739197930890832922599424
7 24650600248172987140112763715584000 -45, 0.000000000024650600248172987140112763715584
8 1080657854354639453670407474439566400000 -51, 0.0000000000010806578543546394536704074744395664
9 47701779391594966287470570490839978880000000 -57, 0.00000000000004770177939159496628747057049083997888
 
Pi to 70 decimal places is:
3.1415926535897932384626433832795028841971693993751058209749445923078164
</pre>
 
=={{header|Julia}}==
2,442

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.