-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise5_14.java
More file actions
65 lines (52 loc) · 1.27 KB
/
Exercise5_14.java
File metadata and controls
65 lines (52 loc) · 1.27 KB
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;
public class Exercise5_14 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter i: ");
int i = input.nextInt();
System.out.println(m(i));
}
public static double m(int n) {
double pi = 0;
double term;
for (int i = 1; i <= n; i += 2) {
term = (1.0 / 2 * i - 1) - (1.0 / 2 * i + 1)
pi = pi + term;
}
return 4 * pi;
}
}
/*public class Exercise5_14 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter i: ");
int i = input.nextInt();
System.out.println(m(i));
System.out.println("i\t\tm(i)");
for (int i = 2; i <= 10000; i++)
System.out.println(i + "\t\t" + m(i));
}
// New solution after 6E, 2/04/07
public static double m(int n) {
double pi = 0;
double term;
for (int i = 1; i <= n; i += 2) {
term = 4.0 * (1.0 / (2 * i - 1) - 1.0 / (2 * i + 1));
pi += term;
}
return pi;
}
// old solution prior to 6E
public static double m(int n) {
double pi = 0;
double term;
int sign = 1;
for (int i = 1; i <= n; i++) {
term = sign * 4.0 / (2 * i - 1);
pi += term;
sign = -1 * sign;
}
return pi;
}
}
*/