Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating pi to nth digit java

I wanted to know how I can generate pi to the nth digit. I have a couple of basic ideas.

  1. Use Math.PI and increase the precision (if that's possible)
  2. Use Euler's formula to generate pi but even here, I would need to increase the precision (I think) Euler's formula for PI
  3. There is also Srinivasa Ramanujan's formula for generating PI which is known for it's rapid convergence. This formula seems difficult to implement. I believe, I would have to also increase deicmal precision here.
    enter image description here

So in short, either way, I would need to increase the precision of BigDecimal depending on what the nth digit is. How would I go about increasing the precision of BigDecimal to nth digit? Also, if there is a better and faster of doing this, can you please point me in the correct direction.

EDIT: I just want to generate PI. I don't want to use for calculations. and this is a question about how I can use BigDecimal to implement my ideas of generating PI.

like image 862
Jeel Shah Avatar asked Dec 03 '11 19:12

Jeel Shah


People also ask

How do you find the nth digit of pi?

Pi = SUMk=0toinfinity 16-k [ 4/(8k+1) – 2/(8k+4) – 1/(8k+5) – 1/(8k+6) ]. The reason this pi formula is so interesting is because it can be used to calculate the N-th digit of Pi (in base 16) without having to calculate all of the previous digits!

How do you generate digits of pi?

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.

How do you declare a pi value in java?

PI” constant of the “java. It is a static double type constant that belongs to the Java Math class: public static final double PI = 3.14159265358979323846; Note: The “final” keyword is utilized for defining a constant that maintains its value and keeps it the same during the execution.

Does java have a pi value?

It is a mathematical constant that is defined as the circumference of a circle divided by its diameter. The value of a constant pi is approximately 3.14. Java provides built-in constant field of Pi that belong to java.


2 Answers

  • Math.PI is of type double. That means about 15 decimal digits of precision, and that is all the data you have; nothing will magically make additional digits of PI appear.
  • BigDecimal has arbitrary precision. setScale() allows you to create BigDecimal objects with as much precision as you want and most of the arithmetic methods will automatically increase precision as required, but of course the more precision, the slower all calculations will be.
  • The most difficult part of implementing Ramanujan's formula will ironically be the sqrt(2) in the constant factor, because there is not built-in sqrt() for BigDecimal, so you'll have to write your own.
like image 105
Michael Borgwardt Avatar answered Oct 25 '22 02:10

Michael Borgwardt


You need to use MathContext to increase the precision of the BigDecimal

e.g.

MathContext mc = new MathContext(1000);
BigDecimal TWO = new BigDecimal(2, mc);

It's important that ALL the BigDecimals you use in your calculations use that MathContext. Heron's method should give you 1000 digits precision with only 10 iterations and a million digits with 20 iterations so it's certainly good enough. Also, create all the constant BigDecimals like e.g. 26390 only once at the start of your program.

like image 24
SpiderPig Avatar answered Oct 25 '22 01:10

SpiderPig