|
2 | 2 |
|
3 | 3 | public class PiNilakantha {
|
4 | 4 |
|
5 | | - // Calculates Pi using Nilakantha's infinite series |
6 | | - // Method 2 in the following link explains the algorithm |
7 | | - //https://en.scratch-wiki.info/wiki/Calculating_Pi |
8 | | - |
| 5 | + // Calculates Pi using Nilakantha's infinite series |
| 6 | + // Method 2 in the following link explains the algorithm |
| 7 | + // https://en.scratch-wiki.info/wiki/Calculating_Pi |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + assert calculatePi(0) == 3.0; |
| 11 | + assert calculatePi(10) > 3.0; |
| 12 | + assert calculatePi(100) < 4.0; |
| 13 | + |
| 14 | + System.out.println(calculatePi(500)); |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * @param iterations number of times the infinite series gets repeated Pi get more accurate the |
| 19 | + * higher the value of iterations is Values from 0 up to 500 are allowed since double |
| 20 | + * precision is not sufficient for more than about 500 repetitions of this algorithm |
| 21 | + * @return the pi value of the calculation with a precision of x iteration |
| 22 | + */ |
| 23 | + public static double calculatePi(int iterations) { |
| 24 | + if (iterations < 0 || iterations > 500) { |
| 25 | + throw new IllegalArgumentException("Please input Integer Number between 0 and 500"); |
| 26 | + } |
9 | 27 |
|
10 | | - public static void main(String[] args) { |
11 | | - assert calculatePi(0) == 3.0; |
12 | | - assert calculatePi(10) > 3.0; |
13 | | - assert calculatePi(100) < 4.0; |
| 28 | + double pi = 3; |
| 29 | + int divCounter = 2; |
14 | 30 |
|
15 | | - System.out.println(calculatePi(500)); |
16 | | - } |
| 31 | + for (int i = 0; i < iterations; i++) { |
17 | 32 |
|
| 33 | + if (i % 2 == 0) pi = pi + 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2)); |
| 34 | + else pi = pi - 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2)); |
18 | 35 |
|
19 | | - /** |
20 | | - * |
21 | | - * @param iterations number of times the infinite series gets repeated |
22 | | - * Pi get more accurate the higher the value of iterations is |
23 | | - * Values from 0 up to 500 are allowed since double precision is not sufficient |
24 | | - * for more than about 500 repetitions of this algorithm |
25 | | - * @return the pi value of the calculation with a precision of x iteration |
26 | | - */ |
27 | | - public static double calculatePi(int iterations) { |
28 | | - if (iterations < 0 || iterations > 500) { |
29 | | - throw new IllegalArgumentException("Please input Integer Number between 0 and 500"); |
30 | | - } |
31 | | - |
32 | | - double pi = 3; |
33 | | - int divCounter = 2; |
34 | | - |
35 | | - for (int i = 0; i < iterations; i++) { |
36 | | - |
37 | | - if (i % 2 == 0) |
38 | | - pi = pi + 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2)); |
39 | | - else |
40 | | - pi = pi - 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2)); |
41 | | - |
42 | | - divCounter += 2; |
43 | | - } |
44 | | - return pi; |
| 36 | + divCounter += 2; |
45 | 37 | }
|
| 38 | + return pi; |
| 39 | + } |
46 | 40 | }
|
0 commit comments