-
Notifications
You must be signed in to change notification settings - Fork 5
/
Pi.java
65 lines (53 loc) · 1.64 KB
/
Pi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.Scanner;
import java.text.DecimalFormat;
/**
* Calculate Pi
* @date 19 August 2014
* @author Nefari0uss
*
* This program will request the approximate number of calculations to run in calculating π.
* The final result will be displayed on the console. Assumption is that the user inputs an int.
*
*
* Uses the Gottfried Leibniz formula for calculation of π:
*
* 1 - 1/3 + 1/5 - 1/7 + 1/9 - ... = π/4
*
* Source: Wikipedia - Leibniz formula for π
**/
public class Pi {
public static void main(String[] args) {
int n = getInput();
double piValue = calculatePi(n);
printResult(piValue);
}
private static double calculatePi(double n) {
double pi = 0;
for (int i = 1; i < n; i++) {
pi += Math.pow(-1,i+1) / (2*i - 1);
}
return 4 * pi;
}
private static int getInput() {
int n = 0;
Scanner input = new Scanner(System.in);
System.out.println("How many calculations should be run for the approximation?");
try {
n = Integer.parseInt(input.nextLine());
} /** Handle input greater than Java's MAX_INT. **/
catch (NumberFormatException e) {
System.out.println("n is too large. Setting n to be the largest possible int value.");
n = Integer.MAX_VALUE;
}
input.close();
return n;
}
private static double calculateError(double piValue) {
return Math.abs(1 - piValue / Math.PI) * 100;
}
private static void printResult(double piValue) {
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("The value of pi is approximately " + piValue + ".");
System.out.println("The calculated value is off by approximately " + df.format(calculateError(piValue)) + "%.");
}
}