-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSpecialSeries.java
More file actions
50 lines (43 loc) · 1.3 KB
/
SpecialSeries.java
File metadata and controls
50 lines (43 loc) · 1.3 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
/**
* Write a method to compute the sum of the series in a class called SpecialSeries.
*
* sum = x + [1/2] * (x<3>/3) + [(1*3)/(2*4)] * (x<5>/5) + [(1*3*5)/(2*4*6)] * (x<7>/7) + ...
*
* The signature of the method is:
* public static double sumOfSeries(double x, int numTerms)
*/
package javaexercises.difficult;
/**
*
* @author User
*/
public class SpecialSeries {
public static void main(String[] args)
{
SpecialSeries aSpecialSeries = new SpecialSeries();
double x = 0.1D;
System.out.printf("Sum of the series: %.9f \n"
, aSpecialSeries.sumOfSeries(x, 5));
}
private double calculateTerm(double x, int numTerms)
{
double term = 1D;
for (int i = (2*numTerms+1); i > 0; i--) {
term *= x*(i%2 == 0 ? 1 : i)/(i%2 == 0 ? i : 1);
}
term /= 2*(2*numTerms+1);
return term;
}
private double sumOfSeries(double x, int numTerms)
{
if (x < -1D || x > 1D) {
System.out.println("Please ensure what x value in range [-1; 1] and try again.");
return 0D;
}
double result = 0D;
for (int i = 0; i < numTerms; i++) {
result += calculateTerm(x, i);
}
return result;
}
}