mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Formatted with Google Java Formatter
This commit is contained in:
@ -4,8 +4,7 @@ public class PiNilakantha {
|
|||||||
|
|
||||||
// Calculates Pi using Nilakantha's infinite series
|
// Calculates Pi using Nilakantha's infinite series
|
||||||
// Method 2 in the following link explains the algorithm
|
// Method 2 in the following link explains the algorithm
|
||||||
//https://en.scratch-wiki.info/wiki/Calculating_Pi
|
// https://en.scratch-wiki.info/wiki/Calculating_Pi
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
assert calculatePi(0) == 3.0;
|
assert calculatePi(0) == 3.0;
|
||||||
@ -15,13 +14,10 @@ public class PiNilakantha {
|
|||||||
System.out.println(calculatePi(500));
|
System.out.println(calculatePi(500));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* @param iterations number of times the infinite series gets repeated Pi get more accurate the
|
||||||
* @param iterations number of times the infinite series gets repeated
|
* higher the value of iterations is Values from 0 up to 500 are allowed since double
|
||||||
* Pi get more accurate the higher the value of iterations is
|
* precision is not sufficient for more than about 500 repetitions of this algorithm
|
||||||
* Values from 0 up to 500 are allowed since double precision is not sufficient
|
|
||||||
* for more than about 500 repetitions of this algorithm
|
|
||||||
* @return the pi value of the calculation with a precision of x iteration
|
* @return the pi value of the calculation with a precision of x iteration
|
||||||
*/
|
*/
|
||||||
public static double calculatePi(int iterations) {
|
public static double calculatePi(int iterations) {
|
||||||
@ -34,10 +30,8 @@ public class PiNilakantha {
|
|||||||
|
|
||||||
for (int i = 0; i < iterations; i++) {
|
for (int i = 0; i < iterations; i++) {
|
||||||
|
|
||||||
if (i % 2 == 0)
|
if (i % 2 == 0) pi = pi + 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2));
|
||||||
pi = pi + 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2));
|
else pi = pi - 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2));
|
||||||
else
|
|
||||||
pi = pi - 4.0 / (divCounter * (divCounter + 1) * (divCounter + 2));
|
|
||||||
|
|
||||||
divCounter += 2;
|
divCounter += 2;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user