This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Meaningless Drivel and the fly likes pi Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Other » Meaningless Drivel
Bookmark "pi" Watch "pi" New topic
Author

pi

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

i am currently doing a little "math program" so i am researching. did you know:
The current record is held by Yasumasa Kanada and Daisuke Takahashi from the University of Tokyo with 51 billion digits of pi (51,539,600,000 decimal digits to be precise).

man! it sure gets complicated from here:
There are essentially 3 different methods to calculate pi to many decimals.

One of the oldest is to use the power series expansion of atan(x) = x - x^3/3 + x^5/5 - ... together with formulas like pi = 16*atan(1/5) - 4*atan(1/239). This gives about 1.4 decimals per term.

A second is to use formulas coming from Arithmetic-Geometric mean computations. A beautiful compendium of such formulas is given in the book pi and the AGM, (see references). They have the advantage of converging quadratically, i.e. you double the number of decimals per iteration. For instance, to obtain 1 000 000 decimals, around 20 iterations are sufficient. The disadvantage is that you need FFT type multiplication to get a reasonable speed, and this is not so easy to program.

A third one comes from the theory of complex multiplication of elliptic curves, and was discovered by S. Ramanujan. This gives a number of beautiful formulas, but the most useful was missed by Ramanujan and discovered by the Chudnovsky's. It is the following (slightly modified for ease of programming):

Set k_1 = 545140134; k_2 = 13591409; k_3 = 640320; k_4 = 100100025; k_5 = 327843840; k_6 = 53360;

Then pi = (k_6 sqrt(k_3))/(S), where

S = sum_(n = 0)^oo (-1)^n ((6n)!(k_2 + nk_1))/(n!^3(3n)!(8k_4k_5)^n)

The great advantages of this formula are that

1) It converges linearly, but very fast (more than 14 decimal digits per term).

2) The way it is written, all operations to compute S can be programmed very simply. This is why the constant 8k_4k_5 appearing in the denominator has been written this way instead of 262537412640768000. This is how the Chudnovsky's have computed several billion decimals.

An interesting new method was recently proposed by David Bailey, Peter Borwein and Simon Plouffe. It can compute the Nth hexadecimal digit of Pi efficiently without the previous N-1 digits. The method is based on the formula:

pi = sum_(i = 0)^oo (1 16^i) ((4 8i + 1) - (2 8i + 4) - (1 8i + 5) - (1 8i + 6))

in O(N) time and O(log N) space. (See references.)

The following 160 character C program, written by Dik T. Winter at CWI, computes pi to 800 decimal digits.

int a=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,
f[b]=d%--g,d/=g--,--b;d*=b);}


SCJP
Visit my download page
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18114
    
    8

Oh... I thought you meant they memorized 51 billion digits of pi.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

roflmao
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

my goal of course is to provide "calculate pi" as an option from my menu. i bet "calculate e" will probably be hard too

any help appreciated

good grief!
The formula for working out the value of e looks fun.

e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! + 1/6! + ....
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

So, if you've just broken the world record for generating digits of pi....

...how do you prove it?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

good question
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

arg! how do i do factorials in java? here i am worried about e when i still haven't figured out pi
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

now i find out about Pascal's triangle....this is going to be fun
Dave Trower
Ranch Hand

Joined: Feb 12, 2003
Posts: 85
I think you need to look at BigInteger and BigDecimal classes that are part of the java.math package. You will need these if you want to calculate e or pi to a large number of places.
I also think e is easier to calculate than pi based on what you listed here. It would be easy to write a function in java that returns a factorial.
If you like this type of thing, you should check out Project Euler at http://projecteuler.net/
Good luck.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

always big numbers represents as a String... not even BigInteger?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Dave Trower wrote:If you like this type of thing, you should check out Project Euler at http://projecteuler.net/

Yes, Project Euler is fun if you like playing with math puzzles and programming. They have a long list of math puzzles which you're supposed to solve by writing small programs. Some of the puzzles are very simple, and some are quite hard. I've used them to learn functional programming in Scala. You can for example compute the Fibonacci sequence in a very nifty way.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1665
    
    7

Wait, not one pie joke? Really?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1511
Is this why my geometry and mensuration bad


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

project euler sound interesting right now. the first thing i did in this program was Fibonacci numbers. i agree calculating e will be easier. i almost have it just from thinking about it. the math for calculating pi looks much harder.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

And today it's Pi Day! (Look at the moose!)


Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Happy Half Tau Day!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pi
 
Similar Threads
How many hours do you sleep every day?
Problem with printf output
class \u03a9\u03bb\u03c0 {} - a valid class name?
Reflections on pi
How to identify some english word is number