-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSinTaylorSeries.java
37 lines (29 loc) ยท 987 Bytes
/
SinTaylorSeries.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
package day3;
import java.util.Scanner;
public class SinTaylorSeries {
/*
sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ....
a(i) = (-1)^รฎ x^(2i + 1) / (2i + 1)!
a(i) = oscillation * factor(x) / factorial
oscillation *= -1
factor *= x^2
factorial *= (2i + 1) * 2i
instantiation --> (condition --> code --> updation)
*/
/*
time Complexity: O(1)
space Complexity: O(1)
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double theta = scanner.nextDouble();
double result = 0;
for (double precision = 0, oscillation = 1, factor = theta, factorial = 1 ; precision < 30 ; precision++) {
result += oscillation * factor / factorial;
oscillation *= -1;
factor *= theta * theta;
factorial *= (2 * precision + 1) * 2 * precision;
}
System.out.println(result);
}
}