Arctan Series

0

0 votes
Problem

The Maclaurin series for arctan(x) is a formula which allows us to compute an approximation to arctan(x) as a polynomial in x. The formula is:

arctan(x) = x - x3/3 + x5/5 - x7/7 + x9/9 - x11/11 + . . .

Write a program, which reads in a double x, and a positive integer k, and prints out the partial sum from the first k terms in this series and also prints out the value of Math.atan(x), which computes the arctangent directly.

Hint #1: If the user enters 0.5 for x and 3 for k, you should compute:

0.5 - 0.53/3 + 0.55/5 = 0.464583....

That is, the first 3 terms of the series with x = 0.5. This turns out to be a good approximation, even with only 3 terms, as:

arctan(0.5) = 0.4636476....


Input Format:
The input contains two numbers X and K respectively.


Output Format:
The result of the equation given an integer X and K

Sample Input
0.5 3
Sample Output
0.4645833333333333
Time Limit: 1
Memory Limit: 256
Source Limit:
Contributers:
Editor Image

?