0
Sum of the series
Write a program to find the sum "S" of the series where S=x-x**3+x**5+..... upto "N" terms. INPUT: The first line of input is an integer "x". The second line of input is an integer N. Explanation: If we take "x" value as '2' in the series upto '5' terms. Then the series is 2 - 2**3 + 2**5 - 2**7 + 2**9 So, the output should be "410". Sample Input 1: 2 5 sample Output 1: 410 Sample input 2: 3 2 Sample Output 2: -24 Write the python code in Answer Box. my code. S=int(input()) N=int(input()) j= S**3 for i in range(0,N): if i%2 == 0: j = j + i else: k = k - i print(j+k)
5 Réponses
+ 4
Vijay Satya Srinivas
s = int(input())
n = int(input())
result = 0
for i in range(1,n+1):
if i%2:
result += s**(i*2-1)
else:
result -= s**(i*2-1)
print(result)
'''You want N iterations, each iteration is to generate an odd number.
So create your loop as
range(1, n+1).
if n = 5, then range will be 1,6 which will produce 1,2,3,4,5
1*2-1 = 1
2*2-1 = 3
3*2-1 = 5
4*2-1 = 7
5*2-1 = 9
We have now created a formula which can generate your required exponent, but now we need to determine when to += the result or -= the result.
Determine if your iteration is odd or even, then apply the appropriate formula by using iteration%2'''
+ 5
If you need help with your code, please LINK your code.
If you think other would do your homework for you: No.
+ 5
Vijay Satya Srinivas
This looks like an interesting challenge / concept, but it also looks like your homework.
Please show your attempt so we may assist.
+ 4
Look at the formula again:
On each iteration of the loop you need to raise x by the power of the next number that is in the 3, 5, 7, ... series. Then you add or subtract this number from your sum. You do this N times
0
where k is not defined