User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

how to make PI out of PI?

Sat Jun 02, 2012 1:24 pm

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.

User avatar
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?

Sat Jun 02, 2012 1:32 pm

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!

spurious
Posts: 343
Joined: Mon Nov 21, 2011 9:29 pm

Re: how to make PI out of PI?

Sat Jun 02, 2012 1:38 pm

One method explained :
http://pcplus.techradar.com/2010/01/17/calculating-pi/


there's probably some example code about too

User avatar
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?

Sun Jun 03, 2012 7:00 am

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! :D
"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!!!

User avatar
Dave_G_2
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm

Re: how to make PI out of PI?

Sun Jun 03, 2012 7:44 am

Why not use bash & bc ?
I don't know if the R-Pi 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
EDIT:

Come to think of it, it could be used for those messing about with over clocking the R-PI
Do a few runs (to get an average) before and the same after over clocking then compare
the reported times.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: how to make PI out of PI?

Sun Jun 03, 2012 9:31 am

That's a perfect example of obtuse programming, Dave :-D

The only important bit is

Code: Select all

echo "scale=$1; 16 * a (1/5) - 4 * a (1/239)" | bc -l
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.

summers
Posts: 63
Joined: Mon Jan 30, 2012 4:27 pm

Re: how to make PI out of PI?

Sun Jun 03, 2012 9:49 am

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) + ... 
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.

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.

User avatar
Dave_G_2
Posts: 196
Joined: Sat Apr 14, 2012 7:04 pm

Re: how to make PI out of PI?

Sun Jun 03, 2012 10:04 am

rurwin wrote:That's a perfect example of obtuse programming, Dave :-D
It gets the job done, adds extra functionality, can be used for checking computational time
and offers an alternative to Python.
It can also be exported to GTKdialog amongst others.

Bakul Shah
Posts: 324
Joined: Sun Sep 25, 2011 1:25 am

Re: how to make PI out of PI?

Sun Jun 03, 2012 11:51 pm

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?? ;)
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!

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("");}

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: how to make PI out of PI?

Mon Jun 04, 2012 12:14 am

And just to get the inevitable "stupid answer" out of the way... :lol:

Code: Select all

python -c 'import math; print math.pi'

User avatar
hexelpdkk
Posts: 177
Joined: Fri Feb 24, 2012 4:40 pm

Re: how to make PI out of PI?

Mon Jun 04, 2012 9:10 am

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!
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.

With default compilation options, "6502 5" gives 9.348s.

User avatar
stephj
Posts: 80
Joined: Thu Jun 21, 2012 1:20 pm
Location: Lancashire, UK

Re: how to make PI out of PI?

Tue Sep 25, 2012 3:40 pm

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.

User avatar
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?

Tue Sep 25, 2012 4:09 pm

3.14159265358979323846

or thereabouts......
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

0117blocky
Posts: 43
Joined: Mon Apr 30, 2012 8:03 am

Re: how to make PI out of PI?

Tue Sep 25, 2012 5:11 pm

quick and dirty method is 355/113 = 3.14159

Man Lee
Posts: 1
Joined: Sun Dec 23, 2012 9:12 am

Re: how to make PI out of PI?

Sun Dec 23, 2012 9:26 am

My C program did 100,000 digits of pi in 113 seconds on my Raspberry Pi.

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: how to make PI out of PI?

Sun Dec 23, 2012 11:44 am

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.
- no just paste up the result :shock:

nr.
Posts: 144
Joined: Wed Oct 03, 2012 8:51 am
Location: The Fens
Contact: Website

Re: how to make PI out of PI?

Sun Dec 23, 2012 8:31 pm

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.
--
nr.

Return to “General discussion”