What's the best software to achieve this and what formula does one use to say calculate PI to 6502 places??

Richard S.
Code: Select all
# pi.py - calculates the constant pi (the ratio of the circumference to the diameter of a circie)
# to any arbitrary number of digits
# Usage: python pi.py number_of_digits - e.g., to calculate pi to 100 digits of accuracy, python pi.py 100
# This program uses Machin's formula (per John Machin circa 1706):
# π = 4 * (4 * arccot(5) - arccot(239))
# where a Taylor series is used to calculate arccot(x) = 1/x - 1/(3x^3) + 1/(5x^5) - 1/(7x^7) + ...
import sys
def arccot(x, unity):
sum = xpower = unity
n = 3
sign = -1
while 1:
xpower = xpower
term = xpower
if not term:
break
sum += sign * term
sign = -sign
n += 2
return sum
def pi(digits):
unity = 10**(digits + 10)
pi = 4 * (4*arccot(5, unity) - arccot(239, unity))
return pi
num_digits = int(sys.argv[1])
result = str(pi(num_digits))
print "3." + result[1:]
Code: Select all
#!/bin/bash
pi()
{
export x=`echo "scale=$scale; 16 * a (1/5) - 4 * a (1/239)" | bc -l`
echo "$x"
}
if [ "$#" = "1" ]
then
scale="$1"
echo `time pi`
else
echo "Usage: $0 #"
fi
Code: Select all
echo "scale=$1; 16 * a (1/5) - 4 * a (1/239)" | bc -l
Yes thats the kind of method I used when hacking this back 25 years ago. Think I may have taked the tan-1(1/5) down to the next level, don't recall expactly - anyway you can transform the inverse tans a few times to get faster convergence, but probably want to keep it as a sum of two inverse tans.Jim Manley wrote:Code: Select all
# pi.py - calculates the constant pi (the ratio of the circumference to the diameter of a circie) # to any arbitrary number of digits # Usage: python pi.py number_of_digits - e.g., to calculate pi to 100 digits of accuracy, python pi.py 100 # This program uses Machin's formula (per John Machin circa 1706): # π = 4 * (4 * arccot(5) - arccot(239)) # where a Taylor series is used to calculate arccot(x) = 1/x - 1/(3x^3) + 1/(5x^5) - 1/(7x^7) + ...
It gets the job done, adds extra functionality, can be used for checking computational timerurwin wrote:That's a perfect example of obtuse programming, Dave![]()
Aubry Jaffer's pi.c is the fastest impementation I know. ftp://ftp.cs.tut.fi/src/languages/schemes/scm/pi.c -- copied below as it is pretty short. On my slightly souped up pi (800Mhz) it takes 11.6 seconds to compute 6502 digits! Compare that with a recent 3.6Ghz Amd8150: 0.43 seconds!redhawk wrote:Everyone knows that PI is a mathematical constant but what I want to know how does one calculate PI using the PI??
What's the best software to achieve this and what formula does one use to say calculate PI to 6502 places??![]()
Code: Select all
/* "pi.c", program for computing digits of numerical value of PI.
Copyright (C) 1991 Aubrey Jaffer.
See the file "COPYING" for terms applying to this program.
(pi <n> <d>) prints out <n> digits of pi in groups of <d> digits.
'Spigot' algorithm origionally due to Stanly Rabinowitz.
This algorithm takes time proportional to the square of <n>/<d>.
This fact can make comparisons of computational speed between systems
of vastly differring performances quicker and more accurate.
Try (pi 100 5)
The digit size <d> will have to be reduced for larger <n> or an
overflow error will occur. */
short *calloc();
main(c,v)
int c;char **v;{
int n=200,j=0,m,b=2,k=0,t,r=1,d=5;
long q;
short *a;
if(c>1)n=atoi(v[1]);
if(c>2)d=atoi(v[2]);
while(k++<d)r=r*10;
n=n/d+1;
k=m=3.322*n*d;
a=calloc(1+m,2);
while(k)a[--k]=2;
for(a[m]=4;j<n;b=q%r){
q=0;
for(k=m;k;){
q+=a[k]*r;
t=(2*k+1);
a[k]=q%t;
q=q/t;
q*=k--;}
printf("%0*d%s",d,b+q/r,++j%10?" ":"\n");}
puts("");}
Code: Select all
python -c 'import math; print math.pi'
To compare with raspbian, I ran this (also with 800Mhz oveclock). When compiled with -Ofast, and running with options "./pi 6502 5", I can get 7.221 seconds. "6502 6" gets it down to 6.043, anything over this hits the overflow issue listed in the source.Bakul Shah wrote: On my slightly souped up pi (800Mhz) it takes 11.6 seconds to compute 6502 digits! Compare that with a recent 3.6Ghz Amd8150: 0.43 seconds!
- no just paste up the resultUsing a similar method I have a C program that calculates pi to a million decimal places
It took 2 days 7 hours. If there is any interest I'll post up the code.