Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/Arithmetic-geometric-mean-Calculate-Pi/00-META.yaml
Normal file
4
Task/Arithmetic-geometric-mean-Calculate-Pi/00-META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Geometry
|
||||
from: http://rosettacode.org/wiki/Arithmetic-geometric_mean/Calculate_Pi
|
||||
18
Task/Arithmetic-geometric-mean-Calculate-Pi/00-TASK.txt
Normal file
18
Task/Arithmetic-geometric-mean-Calculate-Pi/00-TASK.txt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
[http://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/Almkvist-Berndt585-608.pdf Almkvist Berndt 1988] begins with an investigation of why the agm is such an efficient algorithm, and proves that it converges quadratically. This is an efficient method to calculate <math>\pi</math>.
|
||||
|
||||
With the same notations used in [[Arithmetic-geometric mean]], we can summarize the paper by writing:
|
||||
|
||||
<math>\pi =
|
||||
\frac{4\; \mathrm{agm}(1, 1/\sqrt{2})^2}
|
||||
{1 - \sum\limits_{n=1}^{\infty} 2^{n+1}(a_n^2-g_n^2)}
|
||||
</math>
|
||||
|
||||
This allows you to make the approximation, for any large '''N''':
|
||||
|
||||
<math>\pi \approx
|
||||
\frac{4\; a_N^2}
|
||||
{1 - \sum\limits_{k=1}^N 2^{k+1}(a_k^2-g_k^2)}
|
||||
</math>
|
||||
|
||||
The purpose of this task is to demonstrate how to use this approximation in order to compute a large number of decimals of <math>\pi</math>.
|
||||
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
digits = 500
|
||||
an = 1.0
|
||||
bn = sqr(0.5)
|
||||
tn = 0.5 ^ 2
|
||||
pn = 1.0
|
||||
|
||||
while pn <= digits
|
||||
prevAn = an
|
||||
an = (bn + an) / 2
|
||||
bn = sqr(bn * prevAn)
|
||||
prevAn -= an
|
||||
tn -= (pn * prevAn ^ 2)
|
||||
pn *= 2
|
||||
end while
|
||||
print ((an + bn) ^ 2) / (tn * 4)
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
#include <gmpxx.h>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std;
|
||||
using namespace chrono;
|
||||
|
||||
void agm(mpf_class& rop1, mpf_class& rop2, const mpf_class& op1,
|
||||
const mpf_class& op2)
|
||||
{
|
||||
rop1 = (op1 + op2) / 2;
|
||||
rop2 = op1 * op2;
|
||||
mpf_sqrt(rop2.get_mpf_t(), rop2.get_mpf_t());
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
auto st = steady_clock::now();
|
||||
mpf_set_default_prec(300000);
|
||||
mpf_class x0, y0, resA, resB, Z;
|
||||
|
||||
x0 = 1;
|
||||
y0 = 0.5;
|
||||
Z = 0.25;
|
||||
mpf_sqrt(y0.get_mpf_t(), y0.get_mpf_t());
|
||||
|
||||
int n = 1;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
agm(resA, resB, x0, y0);
|
||||
Z -= n * (resA - x0) * (resA - x0);
|
||||
n *= 2;
|
||||
|
||||
agm(x0, y0, resA, resB);
|
||||
Z -= n * (x0 - resA) * (x0 - resA);
|
||||
n *= 2;
|
||||
}
|
||||
|
||||
x0 = x0 * x0 / Z;
|
||||
printf("Took %f ms for computation.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
|
||||
st = steady_clock::now();
|
||||
gmp_printf ("%.89412Ff\n", x0.get_mpf_t());
|
||||
printf("Took %f ms for output.\n", duration<double>(steady_clock::now() - st).count() * 1000.0);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
class AgmPie
|
||||
{
|
||||
static BigInteger IntSqRoot(BigInteger valu, BigInteger guess)
|
||||
{
|
||||
BigInteger term; do {
|
||||
term = valu / guess; if (BigInteger.Abs(term - guess) <= 1) break;
|
||||
guess += term; guess >>= 1;
|
||||
} while (true); return guess;
|
||||
}
|
||||
|
||||
static BigInteger ISR(BigInteger term, BigInteger guess)
|
||||
{
|
||||
BigInteger valu = term * guess; do {
|
||||
if (BigInteger.Abs(term - guess) <= 1) break;
|
||||
guess += term; guess >>= 1; term = valu / guess;
|
||||
} while (true); return guess;
|
||||
}
|
||||
|
||||
static BigInteger CalcAGM(BigInteger lam, BigInteger gm, ref BigInteger z,
|
||||
BigInteger ep)
|
||||
{
|
||||
BigInteger am, zi; ulong n = 1; do {
|
||||
am = (lam + gm) >> 1; gm = ISR(lam, gm);
|
||||
BigInteger v = am - lam; if ((zi = v * v * n) < ep) break;
|
||||
z -= zi; n <<= 1; lam = am;
|
||||
} while (true); return am;
|
||||
}
|
||||
|
||||
static BigInteger BIP(int exp, ulong man = 1)
|
||||
{
|
||||
BigInteger rv = BigInteger.Pow(10, exp); return man == 1 ? rv : man * rv;
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int d = 25000;
|
||||
if (args.Length > 0)
|
||||
{
|
||||
int.TryParse(args[0], out d);
|
||||
if (d < 1 || d > 999999) d = 25000;
|
||||
}
|
||||
DateTime st = DateTime.Now;
|
||||
BigInteger am = BIP(d),
|
||||
gm = IntSqRoot(BIP(d + d - 1, 5),
|
||||
BIP(d - 15, (ulong)(Math.Sqrt(0.5) * 1e+15))),
|
||||
z = BIP(d + d - 2, 25),
|
||||
agm = CalcAGM(am, gm, ref z, BIP(d + 1)),
|
||||
pi = agm * agm * BIP(d - 2) / z;
|
||||
Console.WriteLine("Computation time: {0:0.0000} seconds ",
|
||||
(DateTime.Now - st).TotalMilliseconds / 1000);
|
||||
string s = pi.ToString();
|
||||
Console.WriteLine("{0}.{1}", s[0], s.Substring(1));
|
||||
if (System.Diagnostics.Debugger.IsAttached) Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
#include "gmp.h"
|
||||
|
||||
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
|
||||
mpf_add (out1, in1, in2);
|
||||
mpf_div_ui (out1, out1, 2);
|
||||
mpf_mul (out2, in1, in2);
|
||||
mpf_sqrt (out2, out2);
|
||||
}
|
||||
|
||||
int main (void) {
|
||||
mpf_set_default_prec (300000);
|
||||
mpf_t x0, y0, resA, resB, Z, var;
|
||||
|
||||
mpf_init_set_ui (x0, 1);
|
||||
mpf_init_set_d (y0, 0.5);
|
||||
mpf_sqrt (y0, y0);
|
||||
mpf_init (resA);
|
||||
mpf_init (resB);
|
||||
mpf_init_set_d (Z, 0.25);
|
||||
mpf_init (var);
|
||||
|
||||
int n = 1;
|
||||
int i;
|
||||
for(i=0; i<8; i++){
|
||||
agm(x0, y0, resA, resB);
|
||||
mpf_sub(var, resA, x0);
|
||||
mpf_mul(var, var, var);
|
||||
mpf_mul_ui(var, var, n);
|
||||
mpf_sub(Z, Z, var);
|
||||
n += n;
|
||||
agm(resA, resB, x0, y0);
|
||||
mpf_sub(var, x0, resA);
|
||||
mpf_mul(var, var, var);
|
||||
mpf_mul_ui(var, var, n);
|
||||
mpf_sub(Z, Z, var);
|
||||
n += n;
|
||||
}
|
||||
mpf_mul(x0, x0, x0);
|
||||
mpf_div(x0, x0, Z);
|
||||
gmp_printf ("%.100000Ff\n", x0);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
(ns async-example.core
|
||||
(:use [criterium.core])
|
||||
(:gen-class))
|
||||
|
||||
; Java Arbitray Precision Library
|
||||
(import '(org.apfloat Apfloat ApfloatMath))
|
||||
|
||||
(def precision 8192)
|
||||
|
||||
; Define big constants (i.e. 1, 2, 4, 0.5, .25, 1/sqrt(2))
|
||||
(def one (Apfloat. 1M precision))
|
||||
(def two (Apfloat. 2M precision))
|
||||
(def four (Apfloat. 4M precision))
|
||||
(def half (Apfloat. 0.5M precision))
|
||||
(def quarter (Apfloat. 0.25M precision))
|
||||
(def isqrt2 (.divide one (ApfloatMath/pow two half)))
|
||||
|
||||
(defn compute-pi [iterations]
|
||||
(loop [i 0, n one, [a g] [one isqrt2], z quarter]
|
||||
(if (> i iterations)
|
||||
(.divide (.multiply a a) z)
|
||||
(let [x [(.multiply (.add a g) half) (ApfloatMath/pow (.multiply a g) half)]
|
||||
v (.subtract (first x) a)]
|
||||
(recur (inc i) (.add n n) x (.subtract z (.multiply (.multiply v v) n)))))))
|
||||
|
||||
(doseq [q (partition-all 200 (str (compute-pi 18)))]
|
||||
(println (apply str q)))
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
(load "bf.fasl")
|
||||
|
||||
;;(setf mma::bigfloat-bin-prec 1000)
|
||||
|
||||
(let ((A (mma:bigfloat-convert 1.0d0))
|
||||
(N (mma:bigfloat-convert 1.0d0))
|
||||
(Z (mma:bigfloat-convert 0.25d0))
|
||||
(G (mma:bigfloat-/ (mma:bigfloat-convert 1.0d0)
|
||||
(mma:bigfloat-sqrt (mma:bigfloat-convert 2.0d0)))))
|
||||
(loop repeat 18 do
|
||||
(let* ((X1 (mma:bigfloat-* (mma:bigfloat-+ A G) (mma:bigfloat-convert 0.5d0)))
|
||||
(X2 (mma:bigfloat-sqrt (mma:bigfloat-* A G)))
|
||||
(V (mma:bigfloat-- X1 A)))
|
||||
(setf Z (mma:bigfloat-- Z (mma:bigfloat-* (mma:bigfloat-/ (mma:bigfloat-* V V) (mma:bigfloat-convert 1.0d0)) N) ))
|
||||
(setf N (mma:bigfloat-+ N N))
|
||||
(setf A X1)
|
||||
(setf G X2)))
|
||||
(mma:bigfloat-/ (mma:bigfloat-* A A) Z))
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
import std.bigint;
|
||||
import std.conv;
|
||||
import std.math;
|
||||
import std.stdio;
|
||||
|
||||
BigInt IntSqRoot(BigInt value, BigInt guess) {
|
||||
BigInt term;
|
||||
do {
|
||||
term = value / guess;
|
||||
auto temp = term - guess;
|
||||
if (temp < 0) {
|
||||
temp = -temp;
|
||||
}
|
||||
if (temp <= 1) {
|
||||
break;
|
||||
}
|
||||
guess += term;
|
||||
guess >>= 1;
|
||||
term = value / guess;
|
||||
} while (true);
|
||||
return guess;
|
||||
}
|
||||
|
||||
BigInt ISR(BigInt term, BigInt guess) {
|
||||
BigInt value = term * guess;
|
||||
do {
|
||||
auto temp = term - guess;
|
||||
if (temp < 0) {
|
||||
temp = -temp;
|
||||
}
|
||||
if (temp <= 1) {
|
||||
break;
|
||||
}
|
||||
guess += term;
|
||||
guess >>= 1;
|
||||
term = value / guess;
|
||||
} while (true);
|
||||
return guess;
|
||||
}
|
||||
|
||||
BigInt CalcAGM(BigInt lam, BigInt gm, ref BigInt z, BigInt ep) {
|
||||
BigInt am, zi;
|
||||
ulong n = 1;
|
||||
do {
|
||||
am = (lam + gm) >> 1;
|
||||
gm = ISR(lam, gm);
|
||||
BigInt v = am - lam;
|
||||
if ((zi = v * v * n) < ep) {
|
||||
break;
|
||||
}
|
||||
z -= zi;
|
||||
n <<= 1;
|
||||
lam = am;
|
||||
} while(true);
|
||||
return am;
|
||||
}
|
||||
|
||||
BigInt BIP(int exp, ulong man = 1) {
|
||||
BigInt rv = BigInt(10) ^^ exp;
|
||||
return man == 1 ? rv : man * rv;
|
||||
}
|
||||
|
||||
void main() {
|
||||
int d = 25000;
|
||||
// ignore setting d from commandline for now
|
||||
BigInt am = BIP(d);
|
||||
BigInt gm = IntSqRoot(BIP(d + d - 1, 5), BIP(d - 15, cast(ulong)(sqrt(0.5) * 1e15)));
|
||||
BigInt z = BIP(d + d - 2, 25);
|
||||
BigInt agm = CalcAGM(am, gm, z, BIP(d + 1));
|
||||
BigInt pi = agm * agm * BIP(d - 2) / z;
|
||||
|
||||
string piStr = to!string(pi);
|
||||
writeln(piStr[0], '.', piStr[1..$]);
|
||||
}
|
||||
|
|
@ -0,0 +1,109 @@
|
|||
program Calculate_Pi;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
Velthuis.BigIntegers,
|
||||
System.Diagnostics;
|
||||
|
||||
function IntSqRoot(value, guess: BigInteger): BigInteger;
|
||||
var
|
||||
term: BigInteger;
|
||||
begin
|
||||
while True do
|
||||
begin
|
||||
term := value div guess;
|
||||
if (BigInteger.Abs(term - guess) <= 1) then
|
||||
break;
|
||||
guess := (guess + term) shr 1;
|
||||
end;
|
||||
Result := guess;
|
||||
end;
|
||||
|
||||
function ISR(term, guess: BigInteger): BigInteger;
|
||||
var
|
||||
value: BigInteger;
|
||||
begin
|
||||
value := term * guess;
|
||||
while (True) do
|
||||
begin
|
||||
if (BigInteger.Abs(term - guess) <= 1) then
|
||||
break;
|
||||
guess := (guess + term) shr 1;
|
||||
term := value div guess;
|
||||
end;
|
||||
Result := guess;
|
||||
end;
|
||||
|
||||
function CalcAGM(lam, gm: BigInteger; var z: BigInteger; ep: BigInteger): BigInteger;
|
||||
var
|
||||
am, zi, v: BigInteger;
|
||||
n: UInt32;
|
||||
begin
|
||||
n := 1;
|
||||
while True do
|
||||
begin
|
||||
am := (lam + gm) shr 1;
|
||||
gm := ISR(lam, gm);
|
||||
v := am - lam;
|
||||
zi := v * v * n;
|
||||
if (zi < ep) then
|
||||
break;
|
||||
z := z - zi;
|
||||
n := n shl 1;
|
||||
lam := am;
|
||||
end;
|
||||
Result := am;
|
||||
end;
|
||||
|
||||
function BIP(exp: Integer; man: UInt32 = 1): BigInteger;
|
||||
begin
|
||||
Result := man * BigInteger.Pow(10, exp);
|
||||
end;
|
||||
|
||||
function Compress(val: string; size: Integer): string;
|
||||
begin
|
||||
result := val.Remove(size, val.Length - size * 2).Insert(size, '...');
|
||||
end;
|
||||
|
||||
const
|
||||
DEFAULT_DIGITS = 25000;
|
||||
|
||||
var
|
||||
d: Integer;
|
||||
am, gm, z, agm, pi: BigInteger;
|
||||
StopWatch: TStopwatch;
|
||||
s: string;
|
||||
|
||||
begin
|
||||
StopWatch := TStopwatch.Create;
|
||||
|
||||
d := DEFAULT_DIGITS;
|
||||
if (ParamCount > 0) then
|
||||
begin
|
||||
d := StrToIntDef(ParamStr(1), d);
|
||||
|
||||
if ((d < 1) or (d > 999999)) then
|
||||
d := DEFAULT_DIGITS;
|
||||
end;
|
||||
|
||||
StopWatch.Start;
|
||||
|
||||
am := BIP(d);
|
||||
|
||||
gm := IntSqRoot(BIP(d + d - 1, 5), BIP(d - 15, Trunc(Sqrt(0.5) * 1e+15)));
|
||||
|
||||
z := BIP(d + d - 2, 25);
|
||||
agm := CalcAGM(am, gm, z, BIP(d + 1));
|
||||
|
||||
pi := (agm * agm * BIP(d - 2)) div z;
|
||||
s := pi.ToString.Insert(1, '.');
|
||||
|
||||
StopWatch.Stop;
|
||||
Writeln(Format('Computation time: %.3f seconds ', [StopWatch.ElapsedMilliseconds
|
||||
/ 1000]));
|
||||
|
||||
Writeln(Compress(s, 20));
|
||||
readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
-module(pi).
|
||||
-export([agmPi/1, agmPiBody/5]).
|
||||
|
||||
agmPi(Loops) ->
|
||||
% Tail recursive function that produces pi from the Arithmetic Geometric Mean method
|
||||
A = 1,
|
||||
B = 1/math:sqrt(2),
|
||||
J = 1,
|
||||
Running_divisor = 0.25,
|
||||
A_n_plus_one = 0.5*(A+B),
|
||||
B_n_plus_one = math:sqrt(A*B),
|
||||
Step_difference = A_n_plus_one - A,
|
||||
agmPiBody(Loops-1, Running_divisor-(math:pow(Step_difference, 2)*J), A_n_plus_one, B_n_plus_one, J+J).
|
||||
|
||||
agmPiBody(0, Running_divisor, A, _, _) ->
|
||||
math:pow(A, 2)/Running_divisor;
|
||||
agmPiBody(Loops, Running_divisor, A, B, J) ->
|
||||
A_n_plus_one = 0.5*(A+B),
|
||||
B_n_plus_one = math:sqrt(A*B),
|
||||
Step_difference = A_n_plus_one - A,
|
||||
agmPiBody(Loops-1, Running_divisor-(math:pow(Step_difference, 2)*J), A_n_plus_one, B_n_plus_one, J+J).
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
program CalcPi
|
||||
! Use real128 numbers: (append '_rf')
|
||||
use iso_fortran_env, only: rf => real128
|
||||
implicit none
|
||||
real(rf) :: a,g,s,old_pi,new_pi
|
||||
real(rf) :: a1,g1,s1
|
||||
integer :: k,k1,i
|
||||
|
||||
old_pi = 0.0_rf;
|
||||
a = 1.0_rf; g = 1.0_rf/sqrt(2.0_rf); s = 0.0_rf; k = 0
|
||||
|
||||
do i=1,100
|
||||
call approx_pi_step(a,g,s,k,a1,g1,s1,k1)
|
||||
new_pi = 4.0_rf * (a1**2.0_rf) / (1.0_rf - s1)
|
||||
if (abs(new_pi - old_pi).lt.(2.0_rf*epsilon(new_pi))) then
|
||||
! If the difference between the newly and previously
|
||||
! calculated pi is negligible, stop the calculations
|
||||
exit
|
||||
end if
|
||||
write(*,*) 'Iteration:',k1,' Diff:',abs(new_pi - old_pi),' Pi:',new_pi
|
||||
old_pi = new_pi
|
||||
a = a1; g = g1; s = s1; k = k1
|
||||
end do
|
||||
|
||||
contains
|
||||
|
||||
subroutine approx_pi_step(x,y,z,n,a,g,s,k)
|
||||
real(rf), intent(in) :: x,y,z
|
||||
integer, intent(in) :: n
|
||||
real(rf), intent(out) :: a,g,s
|
||||
integer, intent(out) :: k
|
||||
|
||||
a = 0.5_rf*(x+y)
|
||||
g = sqrt(x*y)
|
||||
k = n + 1
|
||||
s = z + (2.0_rf)**(real(k)+1.0_rf) * (a**(2.0_rf) - g**(2.0_rf))
|
||||
end subroutine
|
||||
end program CalcPi
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
Dim As Short digits = 500
|
||||
Dim As Double an = 1
|
||||
Dim As Double bn = Sqr(0.5)
|
||||
Dim As Double tn = 0.5^2
|
||||
Dim As Double pn = 1
|
||||
Dim As Double prevAn
|
||||
|
||||
While pn <= digits
|
||||
prevAn = an
|
||||
an = (bn + an) / 2
|
||||
bn = Sqr(bn * prevAn)
|
||||
prevAn -= an
|
||||
tn -= (pn * prevAn^2)
|
||||
pn *= 2
|
||||
Wend
|
||||
Dim As Double pi = ((an + bn)^2) / (tn * 4)
|
||||
Print pi
|
||||
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func main() {
|
||||
one := big.NewFloat(1)
|
||||
two := big.NewFloat(2)
|
||||
four := big.NewFloat(4)
|
||||
prec := uint(768) // say
|
||||
|
||||
a := big.NewFloat(1).SetPrec(prec)
|
||||
g := new(big.Float).SetPrec(prec)
|
||||
|
||||
// temporary variables
|
||||
t := new(big.Float).SetPrec(prec)
|
||||
u := new(big.Float).SetPrec(prec)
|
||||
|
||||
g.Quo(a, t.Sqrt(two))
|
||||
sum := new(big.Float)
|
||||
pow := big.NewFloat(2)
|
||||
|
||||
for a.Cmp(g) != 0 {
|
||||
t.Add(a, g)
|
||||
t.Quo(t, two)
|
||||
g.Sqrt(u.Mul(a, g))
|
||||
a.Set(t)
|
||||
pow.Mul(pow, two)
|
||||
t.Sub(t.Mul(a, a), u.Mul(g, g))
|
||||
sum.Add(sum, t.Mul(t, pow))
|
||||
}
|
||||
|
||||
t.Mul(a, a)
|
||||
t.Mul(t, four)
|
||||
pi := t.Quo(t, u.Sub(one, sum))
|
||||
fmt.Println(pi)
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
import java.math.MathContext
|
||||
|
||||
class CalculatePi {
|
||||
private static final MathContext con1024 = new MathContext(1024)
|
||||
private static final BigDecimal bigTwo = new BigDecimal(2)
|
||||
private static final BigDecimal bigFour = new BigDecimal(4)
|
||||
|
||||
private static BigDecimal bigSqrt(BigDecimal bd, MathContext con) {
|
||||
BigDecimal x0 = BigDecimal.ZERO
|
||||
BigDecimal x1 = BigDecimal.valueOf(Math.sqrt(bd.doubleValue()))
|
||||
while (!Objects.equals(x0, x1)) {
|
||||
x0 = x1
|
||||
x1 = (bd.divide(x0, con) + x0).divide(bigTwo, con)
|
||||
}
|
||||
return x1
|
||||
}
|
||||
|
||||
static void main(String[] args) {
|
||||
BigDecimal a = BigDecimal.ONE
|
||||
BigDecimal g = a.divide(bigSqrt(bigTwo, con1024), con1024)
|
||||
BigDecimal t
|
||||
BigDecimal sum = BigDecimal.ZERO
|
||||
BigDecimal pow = bigTwo
|
||||
while (!Objects.equals(a, g)) {
|
||||
t = (a + g).divide(bigTwo, con1024)
|
||||
g = bigSqrt(a * g, con1024)
|
||||
a = t
|
||||
pow = pow * bigTwo
|
||||
sum = sum + (a * a - g * g) * pow
|
||||
}
|
||||
BigDecimal pi = (bigFour * (a * a)).divide(BigDecimal.ONE - sum, con1024)
|
||||
System.out.println(pi)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
import Prelude hiding (pi)
|
||||
import Data.Number.MPFR hiding (sqrt, pi, div)
|
||||
import Data.Number.MPFR.Instances.Near ()
|
||||
|
||||
-- A generous overshoot of the number of bits needed for a
|
||||
-- given number of digits.
|
||||
digitBits :: (Integral a, Num a) => a -> a
|
||||
digitBits n = (n + 1) `div` 2 * 8
|
||||
|
||||
-- Calculate pi accurate to a given number of digits.
|
||||
pi :: Integer -> MPFR
|
||||
pi digits =
|
||||
let eps = fromString ("1e-" ++ show digits)
|
||||
(fromInteger $ digitBits digits) 0
|
||||
two = fromInt Near (getPrec eps) 2
|
||||
twoi = 2 :: Int
|
||||
twoI = 2 :: Integer
|
||||
pis a g s n =
|
||||
let aB = (a + g) / two
|
||||
gB = sqrt (a * g)
|
||||
aB2 = aB ^^ twoi
|
||||
sB = s + (two ^^ n) * (aB2 - gB ^^ twoi)
|
||||
num = 4 * aB2
|
||||
den = 1 - sB
|
||||
in (num / den) : pis aB gB sB (n + 1)
|
||||
puntil f (a:b:xs) = if f a b then b else puntil f (b:xs)
|
||||
in puntil (\a b -> abs (a - b) < eps)
|
||||
$ pis one (one / sqrt two) zero twoI
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
-- The last decimal is rounded.
|
||||
putStrLn $ toString 1000 $ pi 1000
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
DP=: 100
|
||||
|
||||
round=: DP&$: : (4 : 0)
|
||||
b %~ <.1r2+y*b=. 10x^x
|
||||
)
|
||||
|
||||
sqrt=: DP&$: : (4 : 0) " 0
|
||||
assert. 0<:y
|
||||
%/ <.@%: (2 x: (2*x) round y)*10x^2*x+0>.>.10^.y
|
||||
)
|
||||
|
||||
pi =: 3 : 0
|
||||
A =. N =. 1x
|
||||
'G Z HALF' =. (% sqrt 2) , 1r4 1r2
|
||||
for_I. i.18 do.
|
||||
X =. ((A + G) * HALF) , sqrt A * G
|
||||
VAR =. ({.X) - A
|
||||
Z =. Z - VAR * VAR * N
|
||||
N =. +: N
|
||||
'A G' =. X
|
||||
PI =: A * A % Z
|
||||
echo (0j100":PI) , 4 ": I
|
||||
end.
|
||||
PI
|
||||
)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
102j100":<.@o.&.(*&(10^100x))1
|
||||
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
113j111":<.@o.&.(*&(10^111x))1
|
||||
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Calculate_Pi {
|
||||
private static final MathContext con1024 = new MathContext(1024);
|
||||
private static final BigDecimal bigTwo = new BigDecimal(2);
|
||||
private static final BigDecimal bigFour = new BigDecimal(4);
|
||||
|
||||
private static BigDecimal bigSqrt(BigDecimal bd, MathContext con) {
|
||||
BigDecimal x0 = BigDecimal.ZERO;
|
||||
BigDecimal x1 = BigDecimal.valueOf(Math.sqrt(bd.doubleValue()));
|
||||
while (!Objects.equals(x0, x1)) {
|
||||
x0 = x1;
|
||||
x1 = bd.divide(x0, con).add(x0).divide(bigTwo, con);
|
||||
}
|
||||
return x1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
BigDecimal a = BigDecimal.ONE;
|
||||
BigDecimal g = a.divide(bigSqrt(bigTwo, con1024), con1024);
|
||||
BigDecimal t;
|
||||
BigDecimal sum = BigDecimal.ZERO;
|
||||
BigDecimal pow = bigTwo;
|
||||
while (!Objects.equals(a, g)) {
|
||||
t = a.add(g).divide(bigTwo, con1024);
|
||||
g = bigSqrt(a.multiply(g), con1024);
|
||||
a = t;
|
||||
pow = pow.multiply(bigTwo);
|
||||
sum = sum.add(a.multiply(a).subtract(g.multiply(g)).multiply(pow));
|
||||
}
|
||||
BigDecimal pi = bigFour.multiply(a.multiply(a)).divide(BigDecimal.ONE.subtract(sum), con1024);
|
||||
System.out.println(pi);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
# include "rational"; # a reminder
|
||||
|
||||
def pi(precision):
|
||||
(precision | (. + log) | ceil) as $digits
|
||||
| def sq: . as $in | rmult($in; $in) | rround($digits);
|
||||
{an: r(1;1),
|
||||
bn: (r(1;2) | rsqrt($digits)),
|
||||
tn: r(1;4),
|
||||
pn: 1 }
|
||||
| until (.pn > $digits;
|
||||
.an as $prevAn
|
||||
| .an = (rmult(radd(.bn; .an); r(1;2)) | rround($digits) )
|
||||
| .bn = ([.bn, $prevAn] | rmult | rsqrt($digits) )
|
||||
| .tn = rminus(.tn; rmult(rminus($prevAn; .an)|sq; .pn))
|
||||
| .pn *= 2
|
||||
)
|
||||
| rdiv( radd(.an; .bn)|sq; rmult(.tn; 4))
|
||||
| r_to_decimal(precision);
|
||||
|
||||
pi(500)
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
using Printf
|
||||
|
||||
agm1step(x, y) = (x + y) / 2, sqrt(x * y)
|
||||
|
||||
function approxπstep(x, y, z, n::Integer)
|
||||
a, g = agm1step(x, y)
|
||||
k = n + 1
|
||||
s = z + 2 ^ (k + 1) * (a ^ 2 - g ^ 2)
|
||||
return a, g, s, k
|
||||
end
|
||||
|
||||
approxπ(a, g, s) = 4a ^ 2 / (1 - s)
|
||||
|
||||
function testmakepi()
|
||||
setprecision(512)
|
||||
a, g, s, k = BigFloat(1.0), 1 / √BigFloat(2.0), BigFloat(0.0), 0
|
||||
oldπ = BigFloat(0.0)
|
||||
println("Approximating π using ", precision(BigFloat), "-bit floats.")
|
||||
println(" k Error Result")
|
||||
for i in 1:100
|
||||
a, g, s, k = approxπstep(a, g, s, k)
|
||||
estπ = approxπ(a, g, s)
|
||||
if abs(estπ - oldπ) < 2eps(estπ) break end
|
||||
oldπ = estπ
|
||||
err = abs(π - estπ)
|
||||
@printf("%4d%10.1e%68.60e\n", i, err, estπ)
|
||||
end
|
||||
end
|
||||
|
||||
testmakepi()
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
import java.math.BigDecimal
|
||||
import java.math.MathContext
|
||||
|
||||
val con1024 = MathContext(1024)
|
||||
val bigTwo = BigDecimal(2)
|
||||
val bigFour = bigTwo * bigTwo
|
||||
|
||||
fun bigSqrt(bd: BigDecimal, con: MathContext): BigDecimal {
|
||||
var x0 = BigDecimal.ZERO
|
||||
var x1 = BigDecimal.valueOf(Math.sqrt(bd.toDouble()))
|
||||
while (x0 != x1) {
|
||||
x0 = x1
|
||||
x1 = bd.divide(x0, con).add(x0).divide(bigTwo, con)
|
||||
}
|
||||
return x1
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var a = BigDecimal.ONE
|
||||
var g = a.divide(bigSqrt(bigTwo, con1024), con1024)
|
||||
var t : BigDecimal
|
||||
var sum = BigDecimal.ZERO
|
||||
var pow = bigTwo
|
||||
while (a != g) {
|
||||
t = (a + g).divide(bigTwo, con1024)
|
||||
g = bigSqrt(a * g, con1024)
|
||||
a = t
|
||||
pow *= bigTwo
|
||||
sum += (a * a - g * g) * pow
|
||||
}
|
||||
val pi = (bigFour * a * a).divide(BigDecimal.ONE - sum, con1024)
|
||||
println(pi)
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
agm:=proc(n)
|
||||
local a:=1,g:=evalf(sqrt(1/2)),s:=0,p:=4,i;
|
||||
for i to n do
|
||||
a,g:=(a+g)/2,sqrt(a*g);
|
||||
s+=p*(a*a-g*g);
|
||||
p+=p
|
||||
od;
|
||||
4*a*a/(1-s)
|
||||
end:
|
||||
|
||||
Digits:=100000:
|
||||
d:=agm(16)-evalf(Pi):
|
||||
evalf[10](d);
|
||||
# 4.280696926e-89415
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
pi[n_, prec_] :=
|
||||
Module[{a = 1, g = N[1/Sqrt[2], prec], k, s = 0, p = 4},
|
||||
For[k = 1, k < n, k++,
|
||||
{a, g} = {N[(a + g)/2, prec], N[Sqrt[a g], prec]};
|
||||
s += p (a^2 - g^2); p += p]; N[4 a^2/(1 - s), prec]]
|
||||
|
||||
|
||||
pi[7, 100] - N[Pi, 100]
|
||||
1.2026886537*10^-86
|
||||
|
||||
pi[7, 100]
|
||||
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628046852228654
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
from math import sqrt
|
||||
import times
|
||||
|
||||
import bignum
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
|
||||
func sqrt(value, guess: Int): Int =
|
||||
result = guess
|
||||
while true:
|
||||
let term = value div result
|
||||
if abs(term - result) <= 1:
|
||||
break
|
||||
result = (result + term) shr 1
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
|
||||
func isr(term, guess: Int): Int =
|
||||
var term = term
|
||||
result = guess
|
||||
let value = term * result
|
||||
while true:
|
||||
if abs(term - result) <= 1:
|
||||
break
|
||||
result = (result + term) shr 1
|
||||
term = value div result
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
|
||||
func calcAGM(lam, gm: Int; z: var Int; ep: Int): Int =
|
||||
var am: Int
|
||||
var lam = lam
|
||||
var gm = gm
|
||||
var n = 1
|
||||
while true:
|
||||
am = (lam + gm) shr 1
|
||||
gm = isr(lam, gm)
|
||||
let v = am - lam
|
||||
let zi = v * v * n
|
||||
if zi < ep:
|
||||
break
|
||||
dec z, zi
|
||||
inc n, n
|
||||
lam = am
|
||||
result = am
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
|
||||
func bip(exp: int; man = 1): Int {.inline.} = man * pow(10, culong(exp))
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
|
||||
func compress(str: string; size: int): string =
|
||||
if str.len <= 2 * size: str
|
||||
else: str[0..<size] & "..." & str[^size..^1]
|
||||
|
||||
#———————————————————————————————————————————————————————————————————————————————————————————————————
|
||||
|
||||
import os
|
||||
import parseutils
|
||||
import strutils
|
||||
|
||||
const DefaultDigits = 25_000
|
||||
|
||||
var d = DefaultDigits
|
||||
if paramCount() > 0:
|
||||
if paramStr(1).parseInt(d) > 0:
|
||||
if d notin 1..999_999:
|
||||
d = DefaultDigits
|
||||
|
||||
let t0 = getTime()
|
||||
|
||||
let am = bip(d)
|
||||
let gm = sqrt(bip(d + d - 1, 5), bip(d - 15, int(sqrt(0.5) * 1e15)))
|
||||
var z = bip(d + d - 2, 25)
|
||||
let agm = calcAGM(am, gm, z, bip(d + 1))
|
||||
let pi = agm * agm * bip(d - 2) div z
|
||||
|
||||
var piStr = $pi
|
||||
piStr.insert(".", 1)
|
||||
|
||||
let dt = (getTime() - t0).inMicroseconds.float
|
||||
let timestr = if dt > 1_000_000:
|
||||
(dt / 1e6).formatFloat(ffDecimal, 2) & " s"
|
||||
elif dt > 1000:
|
||||
$(dt / 1e3).toInt & " ms"
|
||||
else:
|
||||
$dt.toInt & " µs"
|
||||
echo "Computed ", d, " digits in ", timeStr
|
||||
|
||||
echo "π = ", compress(piStr, 20), "..."
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
let limit = 10000 and n = 2800
|
||||
let x = Array.make (n+1) 2000
|
||||
|
||||
let rec g j sum =
|
||||
if j < 1 then sum else
|
||||
let sum = sum * j + limit * x.(j) in
|
||||
x.(j) <- sum mod (j * 2 - 1);
|
||||
g (j - 1) (sum / (j * 2 - 1))
|
||||
|
||||
let rec f i carry =
|
||||
if i = 0 then () else
|
||||
let sum = g i 0 in
|
||||
Printf.printf "%04d" (carry + sum / limit);
|
||||
f (i - 14) (sum mod limit)
|
||||
|
||||
let () =
|
||||
f n 0;
|
||||
print_newline()
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
pi(n)=my(a=1,g=2^-.5);(1-2*sum(k=1,n,[a,g]=[(a+g)/2,sqrt(a*g)];(a^2-g^2)<<k))^-1*4*a^2
|
||||
pi(6)
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
program AgmForPi;
|
||||
{$mode objfpc}{$h+}{$b-}{$warn 5091 off}
|
||||
uses
|
||||
SysUtils, Math, GMP;
|
||||
|
||||
const
|
||||
MIN_DIGITS = 32;
|
||||
MAX_DIGITS = 1000000;
|
||||
|
||||
var
|
||||
Digits: Cardinal = 256;
|
||||
|
||||
procedure ReadInput;
|
||||
var
|
||||
UserDigits: Cardinal;
|
||||
begin
|
||||
if (ParamCount > 0) and TryStrToDWord(ParamStr(1), UserDigits) then
|
||||
Digits := Min(MAX_DIGITS, Max(UserDigits, MIN_DIGITS));
|
||||
f_set_default_prec(Ceil((Digits + 1)/LOG_10_2));
|
||||
end;
|
||||
|
||||
function Sqrt(a: MpFloat): MpFloat;
|
||||
begin
|
||||
Result := f_sqrt(a);
|
||||
end;
|
||||
|
||||
function Sqr(a: MpFloat): MpFloat;
|
||||
begin
|
||||
Result := a * a;
|
||||
end;
|
||||
|
||||
function PiDigits: string;
|
||||
var
|
||||
a0, b0, an, bn, tn: MpFloat;
|
||||
n: Cardinal;
|
||||
begin
|
||||
n := 1;
|
||||
an := 1;
|
||||
bn := Sqrt(MpFloat(0.5));
|
||||
tn := 0.25;
|
||||
while n < Digits do begin
|
||||
a0 := an;
|
||||
b0 := bn;
|
||||
an := (a0 + b0)/2;
|
||||
bn := Sqrt(a0 * b0);
|
||||
tn := tn - Sqr(an - a0) * n;
|
||||
n := n + n;
|
||||
end;
|
||||
Result := Sqr(an + bn)/(tn * 4);
|
||||
SetLength(Result, Succ(Digits));
|
||||
end;
|
||||
|
||||
begin
|
||||
ReadInput;
|
||||
WriteLn(PiDigits);
|
||||
end.
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
use Math::BigFloat try => "GMP,Pari";
|
||||
|
||||
my $digits = shift || 100; # Get number of digits from command line
|
||||
print agm_pi($digits), "\n";
|
||||
|
||||
sub agm_pi {
|
||||
my $digits = shift;
|
||||
my $acc = $digits + 8;
|
||||
my $HALF = Math::BigFloat->new("0.5");
|
||||
my ($an, $bn, $tn, $pn) = (Math::BigFloat->bone, $HALF->copy->bsqrt($acc),
|
||||
$HALF->copy->bmul($HALF), Math::BigFloat->bone);
|
||||
while ($pn < $acc) {
|
||||
my $prev_an = $an->copy;
|
||||
$an->badd($bn)->bmul($HALF, $acc);
|
||||
$bn->bmul($prev_an)->bsqrt($acc);
|
||||
$prev_an->bsub($an);
|
||||
$tn->bsub($pn * $prev_an * $prev_an);
|
||||
$pn->badd($pn);
|
||||
}
|
||||
$an->badd($bn);
|
||||
$an->bmul($an,$acc)->bdiv(4*$tn, $digits);
|
||||
return $an;
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use Math::BigFloat;
|
||||
|
||||
Math::BigFloat->div_scale(100);
|
||||
|
||||
my $a = my $n = 1;
|
||||
my $g = 1 / sqrt(Math::BigFloat->new(2));
|
||||
my $z = 0.25;
|
||||
for( 0 .. 17 ) {
|
||||
my $x = [ ($a + $g) * 0.5, sqrt($a * $g) ];
|
||||
my $var = $x->[0] - $a;
|
||||
$z -= $var * $var * $n;
|
||||
$n += $n;
|
||||
($a, $g) = @$x;
|
||||
}
|
||||
print $a * $a / $z, "\n";
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
(phixonline)-->
|
||||
<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: #000080;font-style:italic;">-- (mpfr_set_default_prec[ision] has been renamed)</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
|
||||
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">200</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- set precision to 200 decimal places</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.25</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">half</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">x1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">x2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(),</span>
|
||||
<span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #7060A8;">mpfr_sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- g:= 1/sqrt(2)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">18</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">half</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">200</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">=</span><span style="color: #000000;">prev</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</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;">"iteration %d matches previous to %d places\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">exit</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">=</span><span style="color: #000000;">prev</span> <span style="color: #008080;">then</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;">"identical result to last iteration:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">else</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;">"insufficient iterations\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
(scl 40)
|
||||
|
||||
(de pi ()
|
||||
(let
|
||||
(A 1.0 N 1.0 Z 0.25
|
||||
G (/ (* 1.0 1.0) (sqrt 2.0 1.0)) )
|
||||
(use (X1 X2 V)
|
||||
(do 18
|
||||
(setq
|
||||
X1 (/ (* (+ A G) 0.5) 1.0)
|
||||
X2 (sqrt (* A G))
|
||||
V (- X1 A)
|
||||
Z (- Z (/ (* (/ (* V V) 1.0) N) 1.0))
|
||||
N (+ N N)
|
||||
A X1
|
||||
G X2 ) ) )
|
||||
(round (/ (* A A) Z) 40)) )
|
||||
|
||||
(println (pi))
|
||||
|
||||
(bye)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
from decimal import *
|
||||
|
||||
D = Decimal
|
||||
getcontext().prec = 100
|
||||
a = n = D(1)
|
||||
g, z, half = 1 / D(2).sqrt(), D(0.25), D(0.5)
|
||||
for i in range(18):
|
||||
x = [(a + g) * half, (a * g).sqrt()]
|
||||
var = x[0] - a
|
||||
z -= var * var * n
|
||||
n += n
|
||||
a, g = x
|
||||
print(a * a / z)
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
library(Rmpfr)
|
||||
|
||||
agm <- function(n, prec) {
|
||||
s <- mpfr(0, prec)
|
||||
a <- mpfr(1, prec)
|
||||
g <- sqrt(mpfr("0.5", prec))
|
||||
p <- as.bigz(4)
|
||||
for (i in seq(n)) {
|
||||
m <- (a + g) / 2L
|
||||
g <- sqrt(a * g)
|
||||
a <- m
|
||||
s <- s + p * (a * a - g * g)
|
||||
p <- p + p
|
||||
}
|
||||
4L * a * a / (1L - s)
|
||||
}
|
||||
|
||||
1e6 * log(10) / log(2)
|
||||
# [1] 3321928
|
||||
|
||||
# Compute pi to one million decimal digits:
|
||||
p <- 3322000
|
||||
x <- agm(20, p)
|
||||
|
||||
# Check
|
||||
roundMpfr(x - Const("pi", p), 64)
|
||||
# 1 'mpfr' number of precision 64 bits
|
||||
# [1] 4.90382361286485830568e-1000016
|
||||
|
||||
# Save to disk
|
||||
f <- file("pi.txt", "w")
|
||||
writeLines(formatMpfr(x, 1e6), f)
|
||||
close(f)
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/*REXX program calculates the value of pi using the AGM algorithm. */
|
||||
parse arg d .; if d=='' | d=="," then d= 500 /*D not specified? Then use default. */
|
||||
numeric digits d+5 /*set the numeric decimal digits to D+5*/
|
||||
z= 1/4; a= 1; g= sqrt(1/2) /*calculate some initial values. */
|
||||
n= 1
|
||||
do j=1 until a==old; old= a /*keep calculating until no more noise.*/
|
||||
x= (a+g) * .5; g= sqrt(a*g) /*calculate the next set of terms. */
|
||||
z= z - n*(x-a)**2; n= n+n; a= x /*Z is used in the final calculation. */
|
||||
end /*j*/ /* [↑] stop if A equals OLD. */
|
||||
|
||||
pi= a**2 / z /*compute the finished value of pi. */
|
||||
numeric digits d /*set the numeric decimal digits to D.*/
|
||||
say pi / 1 /*display the computed value of pi. */
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric digits; h=d+6
|
||||
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g "E" _ .; g=g *.5'e'_ %2
|
||||
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
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*REXX program calculates the AGM (arithmetic─geometric mean) of two (real) numbers. */
|
||||
parse arg a b digs . /*obtain optional numbers from the C.L.*/
|
||||
if digs=='' | digs=="," then digs= 100 /*No DIGS specified? Then use default.*/
|
||||
numeric digits digs /*REXX will use lots of decimal digits.*/
|
||||
if a=='' | a=="," then a=1 /*No A specified? Then use default.*/
|
||||
if b=='' | b=="," then b=1 / sqrt(2) /*No B specified? " " " */
|
||||
call AGM a,b /*invoke AGM & don't show A,B,result.*/
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
agm: procedure: parse arg x,y; if x=y then return x /*is it an equality case? */
|
||||
if y=0 then return 0 /*is value of Y zero? */
|
||||
if x=0 then return y / 2 /* " " " X " */
|
||||
d= digits(); numeric digits d+5 /*add 5 more digs to ensure convergence*/
|
||||
tiny= '1e-' || (digits() - 1) /*construct a pretty tiny REXX number. */
|
||||
ox= x + 1
|
||||
do #=1 while ox\=x & abs(ox)>tiny; ox= x; oy= y
|
||||
x= (ox+oy)/2; y= sqrt(ox*oy)
|
||||
end /*#*/
|
||||
numeric digits d /*restore numeric digits to original.*/
|
||||
/*this is the only output displayed ►─┐*/
|
||||
say 'digits='right(d, 7)", iterations=" right(#, 3) /* ◄───────────────┘*/
|
||||
return x/1 /*normalize X to the new digits. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
|
||||
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
|
||||
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*/; return g
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/*REXX*/
|
||||
|
||||
Do d=10 To 13
|
||||
Say d pib(d)
|
||||
End
|
||||
Do d=1000 To 1005
|
||||
pi=pib(d)
|
||||
say d left(pi,5)'...'substr(pi,997)
|
||||
End
|
||||
Exit
|
||||
pib: Procedure
|
||||
/* REXX ---------------------------------------------------------------
|
||||
* program calculates the value of pi using the AGM algorithm.
|
||||
* building on top of version 2
|
||||
* reformatted, improved, and using 'my own' sqrt
|
||||
* 08.07.2014 Walter Pachl
|
||||
*--------------------------------------------------------------------*/
|
||||
Parse Arg d .
|
||||
If d=='' Then
|
||||
d=500 /* D specified? Then use default.*/
|
||||
Numeric Digits d+5 /* set the numeric digits to D+5. */
|
||||
a=1
|
||||
n=1
|
||||
z=1/4
|
||||
g=sqrt(1/2) /* calculate some initial values. */
|
||||
Do j=1 Until a==old
|
||||
old=a /* keep calculating until no noise*/
|
||||
x=(a+g)*.5
|
||||
g=sqrt(a*g) /* calculate the next set of terms*/
|
||||
z=z-n*(x-a)**2
|
||||
n=n+n
|
||||
a=x
|
||||
End
|
||||
pi=a**2/z
|
||||
Numeric Digits d /* set the numeric digits to D */
|
||||
Return pi+0
|
||||
|
||||
sqrt: Procedure
|
||||
Parse Arg x
|
||||
xprec=digits()
|
||||
iprec=xprec+10
|
||||
Numeric Digits iprec
|
||||
r0=x
|
||||
r =1
|
||||
Do i=1 By 1 Until r=r0 | (abs(r*r-x)<10**-iprec)
|
||||
r0 = r
|
||||
r = (r + x/r) / 2
|
||||
End
|
||||
Numeric Digits xprec
|
||||
Return (r+0)
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#lang racket
|
||||
(require math/bigfloat)
|
||||
|
||||
(define (pi/a-g rep)
|
||||
(let loop ([a 1.bf]
|
||||
[g (bf1/sqrt 2.bf)]
|
||||
[z (bf/ 1.bf 4.bf)]
|
||||
[n (bf 1)]
|
||||
[r 0])
|
||||
(if (< r rep)
|
||||
(let* ([a-p (bf/ (bf+ a g) 2.bf)]
|
||||
[g-p (bfsqrt (bf* a g))]
|
||||
[z-p (bf- z (bf* (bfsqr (bf- a-p a)) n))])
|
||||
(loop a-p g-p z-p (bf* n 2.bf) (add1 r)))
|
||||
(bf/ (bfsqr a) z))))
|
||||
|
||||
(parameterize ([bf-precision 100])
|
||||
(displayln (bigfloat->string (pi/a-g 5)))
|
||||
(displayln (bigfloat->string pi.bf)))
|
||||
|
||||
(parameterize ([bf-precision 200])
|
||||
(displayln (bigfloat->string (pi/a-g 6)))
|
||||
(displayln (bigfloat->string pi.bf)))
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
constant number-of-decimals = 100;
|
||||
|
||||
multi sqrt(Int $n) {
|
||||
(10**($n.chars div 2), { ($_ + $n div $_) div 2 } ... * == *).tail
|
||||
}
|
||||
|
||||
multi sqrt(FatRat $r --> FatRat) {
|
||||
return FatRat.new:
|
||||
sqrt($r.numerator * 10**(number-of-decimals*2) div $r.denominator),
|
||||
10**number-of-decimals;
|
||||
}
|
||||
|
||||
my FatRat ($a, $n) = 1.FatRat xx 2;
|
||||
my FatRat $g = sqrt(1/2.FatRat);
|
||||
my $z = .25;
|
||||
|
||||
for ^10 {
|
||||
given [ ($a + $g)/2, sqrt($a * $g) ] {
|
||||
$z -= (.[0] - $a)**2 * $n;
|
||||
$n += $n;
|
||||
($a, $g) = @$_;
|
||||
say ($a ** 2 / $z).substr: 0, 2 + number-of-decimals;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
# Calculate Pi using the Arithmetic Geometric Mean of 1 and 1/sqrt(2)
|
||||
#
|
||||
#
|
||||
# Nigel_Galloway
|
||||
# March 8th., 2012.
|
||||
#
|
||||
require 'flt'
|
||||
Flt::BinNum.Context.precision = 8192
|
||||
a = n = 1
|
||||
g = 1 / Flt::BinNum(2).sqrt
|
||||
z = 0.25
|
||||
(0..17).each{
|
||||
x = [(a + g) * 0.5, (a * g).sqrt]
|
||||
var = x[0] - a
|
||||
z -= var * var * n
|
||||
n += n
|
||||
a = x[0]
|
||||
g = x[1]
|
||||
}
|
||||
puts a * a / z
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
/// calculate pi with algebraic/geometric mean
|
||||
pub fn pi(n: usize) -> f64 {
|
||||
let mut a : f64 = 1.0;
|
||||
let two : f64= 2.0;
|
||||
let mut g = 1.0 / two.sqrt();
|
||||
let mut s = 0.0;
|
||||
let mut k = 1;
|
||||
while k<=n {
|
||||
|
||||
let a1 = (a+g)/two;
|
||||
let g1 = (a*g).sqrt();
|
||||
a = a1;
|
||||
g = g1;
|
||||
s += (a.powi(2)-g.powi(2)) * two.powi((k+1) as i32);
|
||||
k += 1;
|
||||
|
||||
|
||||
}
|
||||
|
||||
4.0 * a.powi(2) / (1.0-s)
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println!("pi(7): {}", pi(7));
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
import java.math.MathContext
|
||||
|
||||
import scala.annotation.tailrec
|
||||
import scala.compat.Platform.currentTime
|
||||
import scala.math.BigDecimal
|
||||
|
||||
object Calculate_Pi extends App {
|
||||
val precision = new MathContext(32768 /*65536*/)
|
||||
val (bigZero, bigOne, bigTwo, bigFour) =
|
||||
(BigDecimal(0, precision), BigDecimal(1, precision), BigDecimal(2, precision), BigDecimal(4, precision))
|
||||
|
||||
def bigSqrt(bd: BigDecimal) = {
|
||||
@tailrec
|
||||
def iter(x0: BigDecimal, x1: BigDecimal): BigDecimal =
|
||||
if (x0 == x1) x1 else iter(x1, (bd / x1 + x1) / bigTwo)
|
||||
|
||||
iter(bigZero, BigDecimal(Math.sqrt(bd.toDouble), precision))
|
||||
}
|
||||
|
||||
@tailrec
|
||||
private def loop(a: BigDecimal, g: BigDecimal, sum: BigDecimal, pow: BigDecimal): BigDecimal = {
|
||||
if (a == g) (bigFour * (a * a)) / (bigOne - sum)
|
||||
else {
|
||||
val (_a, _g, _pow) = ((a + g) / bigTwo, bigSqrt(a * g), pow * bigTwo)
|
||||
loop(_a, _g, sum + ((_a * _a - (_g * _g)) * _pow), _pow)
|
||||
}
|
||||
}
|
||||
|
||||
println(precision)
|
||||
val pi = loop(bigOne, bigOne / bigSqrt(bigTwo), bigZero, bigTwo)
|
||||
println(s"This are ${pi.toString.length - 1} digits of π:")
|
||||
val lines = pi.toString().sliding(103, 103).mkString("\n")
|
||||
println(lines)
|
||||
|
||||
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
func agm_pi(digits) {
|
||||
var acc = (digits + 8);
|
||||
|
||||
local Num!PREC = 4*digits;
|
||||
|
||||
var an = 1;
|
||||
var bn = sqrt(0.5);
|
||||
var tn = 0.5**2;
|
||||
var pn = 1;
|
||||
|
||||
while (pn < acc) {
|
||||
var prev_an = an;
|
||||
an = (bn+an / 2);
|
||||
bn = sqrt(bn * prev_an);
|
||||
prev_an -= an;
|
||||
tn -= (pn * prev_an**2);
|
||||
pn *= 2;
|
||||
}
|
||||
|
||||
((an+bn)**2 / 4*tn).to_s
|
||||
}
|
||||
|
||||
say agm_pi(100);
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
3 STO 0 // r0 = 3 (loop count)
|
||||
1 STO 1 STO 3 // r1 = a0, r3 = 1
|
||||
2 √x 1/x STO 2 // r2 = g0
|
||||
. 2 5 STO 4 // r4 = 0.25
|
||||
RCL 1 + x><t RCL 2 = / 2 = // t = a0, x = a1
|
||||
x><t * RCL 2 = √x STO 2 // t = a1, r2 = g1
|
||||
x><t - EXC 1 = // x = (a1 - a0), r1 = a1
|
||||
x² * RCL 3 SUM 3 = INV SUM 4 // r4 = r4-r3(a1-a0)^2, r3 = r3*2
|
||||
RCL 1 x² / RCL 4 = pause
|
||||
dsz 18
|
||||
R/S
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package require math::bigfloat
|
||||
namespace import math::bigfloat::*
|
||||
|
||||
proc agm/π {N {precision 8192}} {
|
||||
set 1 [int2float 1 $precision]
|
||||
set 2 [int2float 2 $precision]
|
||||
set n 1
|
||||
set a $1
|
||||
set g [div $1 [sqrt $2]]
|
||||
set z [div $1 [int2float 4 $precision]]
|
||||
for {set i 0} {$i <= $N} {incr i} {
|
||||
set x0 [div [add $a $g] $2]
|
||||
set x1 [sqrt [mul $a $g]]
|
||||
set var [sub $x0 $a]
|
||||
set z [sub $z [mul [mul $var $n] $var]]
|
||||
incr n $n
|
||||
set a $x0
|
||||
set g $x1
|
||||
}
|
||||
return [tostr [div [mul $a $a] $z]]
|
||||
}
|
||||
|
||||
puts [agm/π 17]
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
LET digits = 500
|
||||
LET an = 1.0
|
||||
LET bn = SQR(0.5)
|
||||
LET tn = 0.5 ^ 2
|
||||
LET pn = 1.0
|
||||
|
||||
DO WHILE pn <= digits
|
||||
LET prevAn = an
|
||||
LET an = (bn + an) / 2
|
||||
LET bn = SQR(bn * prevAn)
|
||||
LET prevAn = prevAn - an
|
||||
LET tn = tn - (pn * prevAn ^ 2)
|
||||
LET pn = pn + pn
|
||||
LOOP
|
||||
PRINT ((an + bn) ^ 2) / (tn * 4)
|
||||
END
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
Imports System, System.Numerics
|
||||
|
||||
Module Program
|
||||
Function IntSqRoot(ByVal valu As BigInteger, ByVal guess As BigInteger) As BigInteger
|
||||
Dim term As BigInteger : Do
|
||||
term = valu / guess
|
||||
If BigInteger.Abs(term - guess) <= 1 Then Exit Do
|
||||
guess += term : guess >>= 1
|
||||
Loop While True : Return guess
|
||||
End Function
|
||||
|
||||
Function ISR(ByVal term As BigInteger, ByVal guess As BigInteger) As BigInteger
|
||||
Dim valu As BigInteger = term * guess : Do
|
||||
If BigInteger.Abs(term - guess) <= 1 Then Exit Do
|
||||
guess += term : guess >>= 1 : term = valu / guess
|
||||
Loop While True : Return guess
|
||||
End Function
|
||||
|
||||
Function CalcAGM(ByVal lam As BigInteger, ByVal gm As BigInteger, ByRef z As BigInteger,
|
||||
ByVal ep As BigInteger) As BigInteger
|
||||
Dim am, zi As BigInteger : Dim n As ULong = 1 : Do
|
||||
am = (lam + gm) >> 1 : gm = ISR(lam, gm)
|
||||
Dim v As BigInteger = am - lam
|
||||
zi = v * v * n : If zi < ep Then Exit Do
|
||||
z -= zi : n <<= 1 : lam = am
|
||||
Loop While True : Return am
|
||||
End Function
|
||||
|
||||
Function BIP(ByVal exp As Integer, ByVal Optional man As ULong = 1) As BigInteger
|
||||
Dim rv As BigInteger = BigInteger.Pow(10, exp) : Return If(man = 1, rv, man * rv)
|
||||
End Function
|
||||
|
||||
Sub Main(args As String())
|
||||
Dim d As Integer = 25000
|
||||
If args.Length > 0 Then
|
||||
Integer.TryParse(args(0), d)
|
||||
If d < 1 OrElse d > 999999 Then d = 25000
|
||||
End If
|
||||
Dim st As DateTime = DateTime.Now
|
||||
Dim am As BigInteger = BIP(d),
|
||||
gm As BigInteger = IntSqRoot(BIP(d + d - 1, 5),
|
||||
BIP(d - 15, Math.Sqrt(0.5) * 1.0E+15)),
|
||||
z As BigInteger = BIP(d + d - 2, 25),
|
||||
agm As BigInteger = CalcAGM(am, gm, z, BIP(d + 1)),
|
||||
pi As BigInteger = agm * agm * BIP(d - 2) / z
|
||||
Console.WriteLine("Computation time: {0:0.0000} seconds ",
|
||||
(DateTime.Now - st).TotalMilliseconds / 1000)
|
||||
If args.Length > 1 OrElse d <= 1000 Then
|
||||
Dim s As String = pi.ToString()
|
||||
Console.WriteLine("{0}.{1}", s(0), s.Substring(1))
|
||||
End If
|
||||
If Diagnostics.Debugger.IsAttached Then Console.ReadKey()
|
||||
End Sub
|
||||
End Module
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import "/big" for BigRat
|
||||
|
||||
var digits = 500
|
||||
var an = BigRat.one
|
||||
var bn = BigRat.half.sqrt(digits)
|
||||
var tn = BigRat.half.square
|
||||
var pn = BigRat.one
|
||||
while (pn <= digits) {
|
||||
var prevAn = an
|
||||
an = (bn + an) * BigRat.half
|
||||
bn = (bn * prevAn).sqrt(digits)
|
||||
prevAn = prevAn - an
|
||||
tn = tn - (prevAn.square * pn)
|
||||
pn = pn + pn
|
||||
}
|
||||
var pi = (an + bn).square / (tn * 4)
|
||||
System.print(pi.toDecimal(digits, false))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
digits = 500
|
||||
an = 1.0
|
||||
bn = sqrt(0.5)
|
||||
tn = 0.5 ^ 2
|
||||
pn = 1.0
|
||||
|
||||
while pn <= digits
|
||||
prevAn = an
|
||||
an = (bn + an) / 2
|
||||
bn = sqrt(bn * prevAn)
|
||||
prevAn = prevAn - an
|
||||
tn = tn - (pn * prevAn ^ 2)
|
||||
pn = pn + pn
|
||||
wend
|
||||
print ((an + bn) ^ 2) / (tn * 4)
|
||||
Loading…
Add table
Add a link
Reference in a new issue