How would you find the Taylor series: sin(x) = x - (x^3/3!) + (x^5/5!) - (x^7/7!) + ...
if you could not use the standard library routines such as exp() or pow() or third party API's?
The input for x is in radians and is from a text file and consists of a single row of values.
If I had the following code, how could i compress it?
You don't have to keep calculating those exponents and factorials from scratch every time, you just use the results from the previous term to help calculate the current term.
For example, just looking at the numerator, you'll calculate:
1 2 3
x^1
x^3 = x^1 * x * x
x^5 = x^3 * x * x
So if you store the result from the previous term, you just multiply by x twice each time.
The denominator is similar.
EDIT: Remember to do the divide using double (not int) or else you'll get a truncated result.