Given a positive integer n and a
real number x, find the value of the sum:
sin x + sin2x + … + sinnx
Input. Two numbers are given: a positive integer n (n ≤ 1000) and a real
number
x (∣x∣ ≤ 1000).
Output. Print the
value of the specified sum, rounded to 6 decimal places.
Sample input |
Sample
output |
3 1 |
2.145368 |
loops
Algorithm analysis
Compute
the specified sum using a loop.
At
the i-th iteration, store the value sinix in the variable t. At the i-th
iteration, add the current term t = sinix to
the current value of the result res.
Algorithm implementation
Read
the input data.
scanf("%d %lf", &n, &x);
Compute
the value of the sum using a loop.
res = 0; t = 1;
for (i = 0; i < n; i++)
{
At
the i-th iteration, it is necessary to multiply t by sin(x), and then add the current term
t = sinix to the result res.
t = t *
sin(x);
res = res + t;
}
Print
the answer.
printf("%lf\n", res);
Python implementation
import math
Read
the input data.
n, x = input().split()
n = int(n)
x = float(x)
Compute
the value of the sum using a loop.
res, t = 0, 1
for i in range(n):
At
the i-th iteration, it is necessary to multiply t
by sin(x), and then add the current term
t = sinix to the result res.
t *= math.sin(x)
res += t
Print
the answer.
print(res)