Competitive Programming Questions Python |
In this article, we will write code to compute the value of sin(x) using Python.
If you are looking for a python tutorial then it's here.
The value of sin(x) may be computed as the sum of the following series:
sin(x) = x - x3/3! + x5/5! - x7/7! + x9/9! + ....
def main(x):
epsilon = 0.00001
multiBy = -x**2
term = x
total = x
nxtInSeq = 2.0
while abs(term) > epsilon:
divBy = nxtInSeq*(nxtInSeq+1)
term = term*multiBy/divBy
total += term
nxtInSeq += 2
print(total)
main(1)
If you run the code, you will get the output:
0.8414710097001764
Comments
Post a Comment