Skip to content

Commit 434320e

Browse files
github-actionsgithub-actions
authored andcommitted
Formatted with Google Java Formatter
1 parent c497d13 commit 434320e

1 file changed

Lines changed: 30 additions & 36 deletions

File tree

Maths/PiNilakantha.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,39 @@
22

33
public class PiNilakantha {
44

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+
}
927

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;
1430

15-
System.out.println(calculatePi(500));
16-
}
31+
for (int i = 0; i < iterations; i++) {
1732

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));
1835

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;
4537
}
38+
return pi;
39+
}
4640
}

0 commit comments

Comments
 (0)