Almkvist-Giullera formula for pi: Difference between revisions

Content added Content deleted
m (argh.)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 34: Line 34:
{{trans|C#}}
{{trans|C#}}


<syntaxhighlight lang=11l>F isqrt(BigInt =x)
<syntaxhighlight lang="11l">F isqrt(BigInt =x)
BigInt q = 1
BigInt q = 1
BigInt r = 0
BigInt r = 0
Line 113: Line 113:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang=AArch64 Assembly>
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program calculPi64.s */
/* program calculPi64.s */
Line 421: Line 421:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=ARM Assembly>
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program calculPi.s */
/* program calculPi.s */
Line 727: Line 727:
{{libheader|System.Numerics}}
{{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).
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).
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using BI = System.Numerics.BigInteger;
using BI = System.Numerics.BigInteger;
using static System.Console;
using static System.Console;
Line 780: Line 780:
{{libheader|Boost}}
{{libheader|Boost}}
{{libheader|GMP}}
{{libheader|GMP}}
<syntaxhighlight lang=cpp>#include <boost/multiprecision/cpp_dec_float.hpp>
<syntaxhighlight lang="cpp">#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/gmp.hpp>
#include <boost/multiprecision/gmp.hpp>
#include <iomanip>
#include <iomanip>
Line 847: Line 847:
{{libheader|computable-reals}}
{{libheader|computable-reals}}
{{trans|Raku}}
{{trans|Raku}}
<syntaxhighlight lang=lisp>(ql:quickload :computable-reals :silent t)
<syntaxhighlight lang="lisp">(ql:quickload :computable-reals :silent t)
(use-package :computable-reals)
(use-package :computable-reals)
(setq *print-prec* 70)
(setq *print-prec* 70)
Line 912: Line 912:
=={{header|dc}}==
=={{header|dc}}==
{{trans|Common Lisp}}
{{trans|Common Lisp}}
<syntaxhighlight lang=dc>[* factorial *]sz
<syntaxhighlight lang="dc">[* factorial *]sz
[ 1 Sp [ d lp * sp 1 - d 1 <f ]Sf d 1 <f Lfsz sz Lp ]sF
[ 1 Sp [ d lp * sp 1 - d 1 <f ]Sf d 1 <f Lfsz sz Lp ]sF


Line 977: 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.
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.
<syntaxhighlight lang=Erlang>
<syntaxhighlight lang="erlang">
-mode(compile).
-mode(compile).


Line 1,105: Line 1,105:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [[Isqrt_(integer_square_root)_of_X#F.23]]
This task uses [[Isqrt_(integer_square_root)_of_X#F.23]]
<syntaxhighlight lang=fsharp>
<syntaxhighlight lang="fsharp">
// Almkvist-Giullera formula for pi. Nigel Galloway: August 17th., 2021
// Almkvist-Giullera formula for pi. Nigel Galloway: August 17th., 2021
let factorial(n:bigint)=MathNet.Numerics.SpecialFunctions.Factorial n
let factorial(n:bigint)=MathNet.Numerics.SpecialFunctions.Factorial n
Line 1,131: Line 1,131:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
{{works with|Factor|0.99 2020-08-14}}
<syntaxhighlight lang=factor>USING: continuations formatting io kernel locals math
<syntaxhighlight lang="factor">USING: continuations formatting io kernel locals math
math.factorials math.functions sequences ;
math.factorials math.functions sequences ;


Line 1,195: Line 1,195:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,285: Line 1,285:
{{libheader|numbers}}
{{libheader|numbers}}
{{trans|Common Lisp}}
{{trans|Common Lisp}}
<syntaxhighlight lang=haskell>import Control.Monad
<syntaxhighlight lang="haskell">import Control.Monad
import Data.Number.CReal
import Data.Number.CReal
import GHC.Integer
import GHC.Integer
Line 1,353: 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.
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>
<syntaxhighlight lang="j">
numerator =: monad define "0
numerator =: monad define "0
(3 * (! x: y)^6) %~ 32 * (!6x*y) * (y*(126 + 532*y)) + 9x
(3 * (! x: y)^6) %~ 32 * (!6x*y) * (y*(126 + 532*y)) + 9x
Line 1,411: Line 1,411:
{{libheader|es-main}} to support use of module as main code
{{libheader|es-main}} to support use of module as main code


<syntaxhighlight lang=javascript>import esMain from 'es-main';
<syntaxhighlight lang="javascript">import esMain from 'es-main';
import { BigFloat, set_precision as SetPrecision } from 'bigfloat-esnext';
import { BigFloat, set_precision as SetPrecision } from 'bigfloat-esnext';


Line 1,491: Line 1,491:


'''Preliminaries'''
'''Preliminaries'''
<syntaxhighlight lang=jq># A reminder to include the "rational" module:
<syntaxhighlight lang="jq"># A reminder to include the "rational" module:
# include "rational";
# include "rational";


Line 1,504: Line 1,504:
end; </syntaxhighlight>
end; </syntaxhighlight>
'''Almkvist-Giullera Formula'''
'''Almkvist-Giullera Formula'''
<syntaxhighlight lang=jq>
<syntaxhighlight lang="jq">
def almkvistGiullera(print):
def almkvistGiullera(print):
. as $n
. as $n
Line 1,518: Line 1,518:
end; </syntaxhighlight>
end; </syntaxhighlight>
'''The Tasks'''
'''The Tasks'''
<syntaxhighlight lang=jq>
<syntaxhighlight lang="jq">
def task1:
def task1:
"N Integer Portion Pow Nth Term",
"N Integer Portion Pow Nth Term",
Line 1,562: Line 1,562:


=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight lang=julia>using Formatting
<syntaxhighlight lang="julia">using Formatting


setprecision(BigFloat, 300)
setprecision(BigFloat, 300)
Line 1,616: Line 1,616:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>ClearAll[numerator, denominator]
<syntaxhighlight lang="mathematica">ClearAll[numerator, denominator]
numerator[n_] := (2^5) ((6 n)!) (532 n^2 + 126 n + 9)/(3 (n!)^6)
numerator[n_] := (2^5) ((6 n)!) (532 n^2 + 126 n + 9)/(3 (n!)^6)
denominator[n_] := 10^(6 n + 3)
denominator[n_] := 10^(6 n + 3)
Line 1,629: Line 1,629:
{{libheader|nim-decimal}}
{{libheader|nim-decimal}}
Derived from Wren version with some simplifications.
Derived from Wren version with some simplifications.
<syntaxhighlight lang=Nim>import strformat, strutils
<syntaxhighlight lang="nim">import strformat, strutils
import decimal
import decimal


Line 1,687: Line 1,687:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<syntaxhighlight lang=perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature qw(say);
use feature qw(say);
Line 1,741: Line 1,741:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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,816: Line 1,816:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp>(scl 70)
<syntaxhighlight lang="picolisp">(scl 70)
(de fact (N)
(de fact (N)
(if (=0 N)
(if (=0 N)
Line 1,860: Line 1,860:


=={{header|Python}}==
=={{header|Python}}==
<syntaxhighlight lang=python>import mpmath as mp
<syntaxhighlight lang="python">import mpmath as mp


with mp.workdps(72):
with mp.workdps(72):
Line 1,914: Line 1,914:
=={{header|Quackery}}==
=={{header|Quackery}}==


<syntaxhighlight lang=Quackery> [ $ "bigrat.qky" loadfile ] now!
<syntaxhighlight lang="quackery"> [ $ "bigrat.qky" loadfile ] now!


[ 1 swap times [ i^ 1+ * ] ] is ! ( n --> n )
[ 1 swap times [ i^ 1+ * ] ] is ! ( n --> n )
Line 1,951: Line 1,951:


=={{header|Raku}}==
=={{header|Raku}}==
<syntaxhighlight lang=perl6># 20201013 Raku programming solution
<syntaxhighlight lang="raku" line># 20201013 Raku programming solution


use BigRoot;
use BigRoot;
Line 1,999: Line 1,999:


=={{header|REXX}}==
=={{header|REXX}}==
<syntaxhighlight lang=rexx>/*REXX program uses the Almkvist─Giullera formula for 1 / (pi**2) [or pi ** -2]. */
<syntaxhighlight lang="rexx">/*REXX program uses the Almkvist─Giullera formula for 1 / (pi**2) [or pi ** -2]. */
numeric digits length( pi() ) + length(.); w= 102
numeric digits length( pi() ) + length(.); w= 102
say $( , 3) $( , w%2) $('power', 5) $( , w)
say $( , 3) $( , w%2) $('power', 5) $( , w)
Line 2,069: Line 2,069:


=={{header|Sidef}}==
=={{header|Sidef}}==
<syntaxhighlight lang=ruby>func almkvist_giullera(n) {
<syntaxhighlight lang="ruby">func almkvist_giullera(n) {
(32 * (14*n * (38*n + 9) + 9) * (6*n)!) / (3 * n!**6)
(32 * (14*n * (38*n + 9) + 9) * (6*n)!) / (3 * n!**6)
}
}
Line 2,118: Line 2,118:
{{libheader|System.Numerics}}
{{libheader|System.Numerics}}
{{trans|C#}}
{{trans|C#}}
<syntaxhighlight lang=vbnet>Imports System, BI = System.Numerics.BigInteger, System.Console
<syntaxhighlight lang="vbnet">Imports System, BI = System.Numerics.BigInteger, System.Console


Module Module1
Module Module1
Line 2,178: Line 2,178:
{{libheader|Wren-big}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang=ecmascript>import "/big" for BigInt, BigRat
<syntaxhighlight lang="ecmascript">import "/big" for BigInt, BigRat
import "/fmt" for Fmt
import "/fmt" for Fmt