-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHarmonicSum.java
More file actions
56 lines (48 loc) · 1.43 KB
/
HarmonicSum.java
File metadata and controls
56 lines (48 loc) · 1.43 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
package javaexercises.flowcontrol;
/**
* Write a program called HarmonicSum to compute the sum of a harmonic series,
* as shown below, where n=50000. The program shall compute the sum
* from left-to-right as well as from the right-to-left.
* Obtain the difference between these two sums and explain the difference.
*
* Harmonic(n) = 1 + 1/2 + 1/3 + ... 1/n
*/
/**
*
* @author User
*/
public class HarmonicSum {
public static void main(String[] args) {
int n = 50000;
HarmonicSum aHarmonicSum = new HarmonicSum();
double sumL2R = aHarmonicSum.printLeftToRightSum(n);
double sumR2L = aHarmonicSum.printRightToLeftSum(n);
System.out.printf("Difference: %.15f", (sumL2R - sumR2L));
System.out.println();
}
/**
* Left-to-right harmonic sum.
*
* @param int n
*/
private double printLeftToRightSum(int n)
{
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += (double) 1/i;
}
System.out.printf("Left-to-right harmonic sum %.15f", sum);
System.out.println();
return sum;
}
private double printRightToLeftSum(int n)
{
double sum = 0.0;
for (int i = n; i >= 1; i--) {
sum += (double) 1/i;
}
System.out.printf("Right-to-left harmonic sum %.15f", sum);
System.out.println();
return sum;
}
}