forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHarmonicNumber.java
More file actions
36 lines (33 loc) · 1.1 KB
/
HarmonicNumber.java
File metadata and controls
36 lines (33 loc) · 1.1 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
package com.thealgorithms.maths;
/**
* A class for calculating harmonic numbers.
* The harmonic series is the divergent infinite series: 1 + 1/2 + 1/3 + 1/4 + ...
*/
public final class HarmonicNumber {
private HarmonicNumber() {}
/**
* Calculates the nth harmonic number.
* The harmonic series is the sum: 1 + 1/2 + 1/3 + ... + 1/n
* This series appears in various mathematical and practical applications.
*
* For example:
* H(1) = 1
* H(2) = 1 + 1/2 = 1.5
* H(3) = 1 + 1/2 + 1/3 ≈ 1.833
* H(4) = 1 + 1/2 + 1/3 + 1/4 ≈ 2.083
*
* @param numTerms the number of terms to sum (n)
* @return the nth harmonic number
* @throws IllegalArgumentException if numTerms is less than 1
*/
public static double of(int numTerms) {
if (numTerms < 1) {
throw new IllegalArgumentException("Number of terms must be positive");
}
double harmonicSum = 0.0;
for (int termIndex = 1; termIndex <= numTerms; termIndex++) {
harmonicSum += 1.0 / termIndex;
}
return harmonicSum;
}
}