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??
Richard S.
 soslug
 Posts: 43
 Joined: Mon May 21, 2012 12:31 pm
 Location: Southend on Sea
 Contact: Website Yahoo Messenger
Re: how to make PI out of PI?
Is the circumference divided by the radius not sure it will go that high though
IPFreely, to weed the network demons that dwell in them their Windows!
Re: how to make PI out of PI?
One method explained :
http://pcplus.techradar.com/2010/01/17/calculatingpi/
there's probably some example code about too
http://pcplus.techradar.com/2010/01/17/calculatingpi/
there's probably some example code about too
 Jim Manley
 Posts: 1600
 Joined: Thu Feb 23, 2012 8:41 pm
 Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
 Contact: Website
Re: how to make PI out of PI?
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:]
The best things in life aren't things ... but, a Pi comes pretty darned close!
"Education is not the filling of a pail, but the lighting of a fire."  W.B. Yeats
In theory, theory & practice are the same  in practice, they aren't!!!
"Education is not the filling of a pail, but the lighting of a fire."  W.B. Yeats
In theory, theory & practice are the same  in practice, they aren't!!!
Re: how to make PI out of PI?
Why not use bash & bc ?
I don't know if the RPi has bc.
This is the script I found on my Linux box.
Save the script below as pi.sh and give it exec permissions:
Then type:
./pi.sh NumDecimalPoints (where NumDecimalPoints is er...well self explanatory)
Keep in mind that a number like 5000 took 35.783 sec on my 1.6GHz ATOM with 2GB RAM.
EDIT:
Come to think of it, it could be used for those messing about with over clocking the RPI
Do a few runs (to get an average) before and the same after over clocking then compare
the reported times.
I don't know if the RPi has bc.
This is the script I found on my Linux box.
Save the script below as pi.sh and give it exec permissions:
Then type:
./pi.sh NumDecimalPoints (where NumDecimalPoints is er...well self explanatory)
Keep in mind that a number like 5000 took 35.783 sec on my 1.6GHz ATOM with 2GB RAM.
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
Come to think of it, it could be used for those messing about with over clocking the RPI
Do a few runs (to get an average) before and the same after over clocking then compare
the reported times.
Re: how to make PI out of PI?
That's a perfect example of obtuse programming, Dave
The only important bit is
and for those trying to follow it, $1 is the first parameter to the script, the variable "scale" sets the number of decimal places, and the function "a" is arctan.
The only important bit is
Code: Select all
echo "scale=$1; 16 * a (1/5)  4 * a (1/239)"  bc l
Re: how to make PI out of PI?
Yes thats the kind of method I used when hacking this back 25 years ago. Think I may have taked the tan1(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) + ...
When going to high dp, you usually need to go to a handful of extra dp, then throw away the last few  due to rownding errors.
Can remember where I got to back when, think I was well over the millions of dp  can't remember if I reached a billion.
Re: how to make PI out of PI?
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
and offers an alternative to Python.
It can also be exported to GTKdialog amongst others.

 Posts: 324
 Joined: Sun Sep 25, 2011 1:25 am
Re: how to make PI out of PI?
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("");}
Re: how to make PI out of PI?
And just to get the inevitable "stupid answer" out of the way...
Code: Select all
python c 'import math; print math.pi'
Re: how to make PI out of 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!
With default compilation options, "6502 5" gives 9.348s.
Re: how to make PI out of PI?
Using 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.
As the program executes the multiple precision arrays start to fill up with zeros as the answer gets more accurate. It uses a running total of log10 of the divisor to figure out out how many of the elements are zero and starts/ends processing at that point. The processing of the arrays runs faster and faster as the program runs.
A similar program runs on a 2.6 Ghz Quad Core PC in around 15 minutes. Having said that, it spawns independant threads across all four cores, and also runs under the 64 bit version of Windows. It uses x64 assembler instructions to multiply two int64 values into an int128 product. That way a single int64 element can store 18 digits of the final answer.
Hardly comparing like with like. Our 700Mhz 32 bit Pi has to do it the hard way.
It took 2 days 7 hours. If there is any interest I'll post up the code.
As the program executes the multiple precision arrays start to fill up with zeros as the answer gets more accurate. It uses a running total of log10 of the divisor to figure out out how many of the elements are zero and starts/ends processing at that point. The processing of the arrays runs faster and faster as the program runs.
A similar program runs on a 2.6 Ghz Quad Core PC in around 15 minutes. Having said that, it spawns independant threads across all four cores, and also runs under the 64 bit version of Windows. It uses x64 assembler instructions to multiply two int64 values into an int128 product. That way a single int64 element can store 18 digits of the final answer.
Hardly comparing like with like. Our 700Mhz 32 bit Pi has to do it the hard way.
 pluggy
 Posts: 3635
 Joined: Thu May 31, 2012 3:52 pm
 Location: Barnoldswick, Lancashire,UK
 Contact: Website
Re: how to make PI out of PI?
3.14159265358979323846
or thereabouts......
or thereabouts......
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......
I must not tread on too many sacred cows......

 Posts: 43
 Joined: Mon Apr 30, 2012 8:03 am
Re: how to make PI out of PI?
quick and dirty method is 355/113 = 3.14159
Re: how to make PI out of PI?
My C program did 100,000 digits of pi in 113 seconds on my Raspberry Pi.

 Posts: 1562
 Joined: Sun Mar 04, 2012 12:49 am
Re: how to make PI out of PI?
 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.
Re: how to make PI out of PI?
I've done a bit of playing around with this: http://www.raspberrypi.org/phpBB3/viewt ... 14&p=80261
The code for pi_css5 is generally available  I've been playing with it for a while on Raspbian and RISC OS for the pi as a general test case once I've got "hello world" up and running.
The code for pi_css5 is generally available  I've been playing with it for a while on Raspbian and RISC OS for the pi as a general test case once I've got "hello world" up and running.

nr.
nr.