Almkvist-Giullera formula for pi: Difference between revisions
m
syntax highlighting fixup automation
(add PicoLisp) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7:
The formula is:
::::: <big><big>1/π<sup>2</sup> = (2<
Line 34:
{{trans|C#}}
<
BigInt q = 1
BigInt r = 0
Line 92:
R s[0]‘.’s[1 .+ digs]
print(dump(70, 1B))</
{{out}}
Line 113:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program calculPi64.s */
Line 402:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
Program 64 bits start.
Line 421:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<
/* ARM assembly Raspberry PI */
/* program calculPi.s */
Line 708:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
96
Line 727:
{{libheader|System.Numerics}}
A little challenging due to lack of BigFloat or BigRational. Note the extended precision integers displayed for each term, not extended precision floats. Also features the next term based on the last term, rather than computing each term from scratch. And the multiply by 32, divide by 3 is reserved for final sum, instead of each term (except for the 0..9th displayed terms).
<
using BI = System.Numerics.BigInteger;
using static System.Console;
Line 759:
static void Main(string[] args) {
WriteLine(dump(70, true)); }
}</
{{out}}
<pre> 0 9600000000000000000000000000000000000000000000000000000000000
Line 780:
{{libheader|Boost}}
{{libheader|GMP}}
<
#include <boost/multiprecision/gmp.hpp>
#include <iomanip>
Line 823:
std::cout << "\nPi to 70 decimal places is:\n"
<< std::fixed << std::setprecision(70) << pi << '\n';
}</
{{out}}
Line 847:
{{libheader|computable-reals}}
{{trans|Raku}}
<
(use-package :computable-reals)
(setq *print-prec* 70)
Line 891:
(format t "~%~%Pi after ~a iterations: " *iterations*)
(print-r (almkvist-giullera-pi *iterations*) *print-prec*)
</syntaxhighlight>
{{Out}}
Line 912:
=={{header|dc}}==
{{trans|Common Lisp}}
<
[ 1 Sp [ d lp * sp 1 - d 1 <f ]Sf d 1 <f Lfsz sz Lp ]sF
Line 954:
[* print resulting value of pi to 70 places *]sz
[Pi after ]n 52n [ iterations:]p
99k 52 lPx 70k 1 / p</
{{Out}}
Line 977:
However, Erlang does not have much in the way of calculating with large integers beyond basic arithmetic, so this version implements integer powers, logs, square roots, as well as the factorial function.
<
-mode(compile).
Line 1,082:
io:format("~npi to 70 decimal places:~n"),
io:format("~s~n", [Pi70]).
</syntaxhighlight>
{{Out}}
<pre>
Line 1,105:
=={{header|F_Sharp|F#}}==
This task uses [[Isqrt_(integer_square_root)_of_X#F.23]]
<
// Almkvist-Giullera formula for pi. Nigel Galloway: August 17th., 2021
let factorial(n:bigint)=MathNet.Numerics.SpecialFunctions.Factorial n
Line 1,112:
let _,n=Seq.unfold(fun(n,g)->let n=n*(10I**6)+fN g in Some(Isqrt((10I**(145+6*(int g)))/(32I*n)),(n,g+1I)))(0I,0I)|>Seq.pairwise|>Seq.find(fun(n,g)->n=g)
printfn $"""pi to 70 decimal places is %s{(n.ToString()).Insert(1,".")}"""
</syntaxhighlight>
{{out}}
<pre>
Line 1,131:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<
math.factorials math.functions sequences ;
Line 1,173:
"%d %44d %3d %.33f\n" printf
] each-integer nl
"Pi to 70 decimal places:" print pi</
{{out}}
<pre>
Line 1,195:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 1,261:
fmt.Println("\nPi to 70 decimal places is:")
fmt.Println(pi.Text('f', 70))
}</
{{out}}
Line 1,285:
{{libheader|numbers}}
{{trans|Common Lisp}}
<
import Data.Number.CReal
import GHC.Integer
Line 1,331:
powInteger b 1 = b
powInteger b e = b `timesInteger` powInteger b (e `minusInteger` 1)
</syntaxhighlight>
{{Out}}
Line 1,353:
sqrt is noticeably slow, bringing 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.
<syntaxhighlight lang=J>
numerator =: monad define "0
(3 * (! x: y)^6) %~ 32 * (!6x*y) * (y*(126 + 532*y)) + 9x
Line 1,385:
echo 'pi to 70 decimals: ', 0j70 ": pi70
exit ''
</syntaxhighlight>
{{Out}}
<pre>
Line 1,411:
{{libheader|es-main}} to support use of module as main code
<
import { BigFloat, set_precision as SetPrecision } from 'bigfloat-esnext';
Line 1,466:
if (esMain(import.meta))
demo();
</syntaxhighlight>
{{Out}}
Line 1,491:
'''Preliminaries'''
<
# include "rational";
Line 1,502:
if . < 2 then 1
else reduce range(2;.+1) as $i (1; .*$i)
end; </
'''Almkvist-Giullera Formula'''
<syntaxhighlight lang=jq>
def almkvistGiullera(print):
. as $n
Line 1,516:
then "\($n|lpad(2)) \($ip|lpad(44)) \(-$pw|lpad(3)), \($tm|r_to_decimal(100))"
else $tm
end; </
'''The Tasks'''
<syntaxhighlight lang=jq>
def task1:
"N Integer Portion Pow Nth Term",
Line 1,541:
task1,
""
task2(70)</
{{out}}
<pre>
Line 1,562:
=={{header|Julia}}==
<
setprecision(BigFloat, 300)
Line 1,596:
println("Computer π is ", format(π + big"0.0", precision=70))
</
<pre>
N Integer Term Power of 10 Nth Term
Line 1,616:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
numerator[n_] := (2^5) ((6 n)!) (532 n^2 + 126 n + 9)/(3 (n!)^6)
denominator[n_] := 10^(6 n + 3)
numerator /@ Range[0, 9]
val = 1/Sqrt[Total[numerator[#]/denominator[#] & /@ Range[0, 100]]];
N[val, 70]</
{{out}}
<pre>{96,5122560,190722470400,7574824857600000,312546150372456000000,13207874703225491420651520,567273919793089083292259942400,24650600248172987140112763715584000,1080657854354639453670407474439566400000,47701779391594966287470570490839978880000000}
Line 1,629:
{{libheader|nim-decimal}}
Derived from Wren version with some simplifications.
<
import decimal
Line 1,666:
inc n
let pi = 1 / sqrt(sum)
echo ($pi)[0..71]</
{{out}}
Line 1,687:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use feature qw(say);
Line 1,724:
printf("π to %s decimal places is:\n%s\n",
$precision, almkvist_giullera_pi($precision));</
{{out}}
<pre>First 10 integer portions:
Line 1,741:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span>
Line 1,793:
<span style="color: #7060A8;">mpfr_const_pi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Pi (builtin) : %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">70</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 1,816:
=={{header|PicoLisp}}==
<
(de fact (N)
(if (=0 N)
Line 1,841:
(setq S (sqrt (*/ 1.0 1.0 S) 1.0))
(prinl "Pi to 70 decimal places is:")
(prinl (format S *Scl)) )</
{{out}}
<pre>
Line 1,860:
=={{header|Python}}==
<
with mp.workdps(72):
Line 1,895:
print('mpmath π is ', end='')
mp.nprint(mp.pi, 71)
</
<pre>
Term 0 96
Line 1,914:
=={{header|Quackery}}==
<
[ 1 swap times [ i^ 1+ * ] ] is ! ( n --> n )
Line 1,932:
53 times [ i^ vterm v+ ]
1/v 70 vsqrt drop
70 point$ echo$ cr</
{{out}}
Line 1,951:
=={{header|Raku}}==
<
use BigRoot;
Line 1,982:
loop { $target eq ( my $next = Pi ++$Nth ) ?? ( last ) !! $target = $next }
say "π to $Precision decimal places is :\n$target"</
{{out}}
<pre>First 10 integer portions :
Line 1,999:
=={{header|REXX}}==
<
numeric digits length( pi() ) + length(.); w= 102
say $( , 3) $( , w%2) $('power', 5) $( , w)
Line 2,036:
do j=0 while h>9; m.j= h; h= h % 2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g= (g + x/g) * .5; end /*k*/
numeric digits d; return g/1</
{{out|output|text= when using the internal default input:}}
(Shown at two─thirds size.)
Line 2,069:
=={{header|Sidef}}==
<
(32 * (14*n * (38*n + 9) + 9) * (6*n)!) / (3 * n!**6)
}
Line 2,097:
say "π to #{n} decimal places is:"
say almkvist_giullera_pi(n)
}</
{{out}}
<pre>
Line 2,118:
{{libheader|System.Numerics}}
{{trans|C#}}
<
Module Module1
Line 2,158:
End Sub
End Module</
{{out}}
<pre> 0 9600000000000000000000000000000000000000000000000000000000000
Line 2,178:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 2,221:
var pi = BigRat.one/sum.sqrt(70)
System.print("\nPi to 70 decimal places is:")
System.print(pi.toDecimal(70))</
{{out}}
|