+ 2

How to find sum of the series 2+ 10+ 18+.... n in Python?

I'm a new learner and wondering how to do it. I can do it in c programming

2nd Dec 2019, 3:58 PM
Ishmum Tahzeeb
Ishmum Tahzeeb - avatar
17 Antworten
+ 6
You just need to use range() and sum() : result = sum(range(2, n, 8)) where n is the ending value. In case you don't know, the third argument of range() is optional (it defaults to 1) and it is the step used to output values from 2 to n, giving you 2, 2+8, 2+8+8,..., until the last 2+8k that is smaller than n. Happy coding!
2nd Dec 2019, 5:52 PM
Gonçalo Magalhaes
Gonçalo Magalhaes - avatar
+ 1
Gonçalo Magalhaes thank you so much man ❤️
2nd Dec 2019, 5:56 PM
Ishmum Tahzeeb
Ishmum Tahzeeb - avatar
+ 1
You're welcome! Glad I could help :)
2nd Dec 2019, 5:58 PM
Gonçalo Magalhaes
Gonçalo Magalhaes - avatar
+ 1
Ya you had i better answer. Didn't think of that.
2nd Dec 2019, 6:07 PM
XXX
XXX - avatar
+ 1
The easiest solution is this: num_list=[2, 4,3,5,1] sum(num_list) # returns sum of the number elements of the num_list.
3rd Dec 2019, 3:57 AM
Ishmam
Ishmam - avatar
3rd Dec 2019, 4:00 AM
Ishmum Tahzeeb
Ishmum Tahzeeb - avatar
+ 1
Unless Python optimizes for this, it might be faster computationally to use the mathematical shortcut for an arithmetic series Sq= q*(a1 + aq) /2. In this case the nth term, n = 2+(q-1)*8. So with the correction from thoq! below, result=((n + 4)**2 - 4) /16 Here's code showing equivalence to sum(range(2,1000,8)), up to n=994. I haven't timed the two methods yet: https://code.sololearn.com/c9XMNrJqtmRf/?ref=app Or in terms of the qth term: result= (4 *q**2)-2*q I'll time this at some point to see if there's any speed difference compared to writing out the sum of a range. I'm guessing there won't be much. https://m.wikihow.com/Find-the-Sum-of-an-Arithmetic-Sequence
3rd Dec 2019, 4:32 PM
Hope J
+ 1
Hope J Your formula doesn't seem to work. This one does (I am sure it could be simplified): res = 8*(((n-2)/8 + 1)*(n-2)/8/2)+2*((n-2)/8)+2 Gonçalo Magalhaes It needs to be like this: sum(range(2,n+1,8)) Otherwise n is excluded from the sum.
3rd Dec 2019, 7:41 PM
Thoq!
Thoq! - avatar
+ 1
x=0 n=int(input("Enter the number")) for a in range(2,n+1,8): x=x+a print(x)
4th Dec 2019, 1:50 PM
Anuj Singh
Anuj Singh - avatar
0
If it's a string you can use the eval() function
2nd Dec 2019, 4:36 PM
‎‫omri
‎‫omri - avatar
0
not string, i want the sum
2nd Dec 2019, 4:54 PM
Ishmum Tahzeeb
Ishmum Tahzeeb - avatar
0
I will not give you the exact code. What you do is 1. Define an empty list and the variable, say x, with value 2 2. While loop till x is not equal to n, adding 8 to x each time. 3. In each loop, add the new value of x to the list 4. In the end, find the sum of the list Hope I was clear and also that i understood what you want correctly
2nd Dec 2019, 4:56 PM
XXX
XXX - avatar
0
Thank you very much XXX, but could you please write the code?
2nd Dec 2019, 5:02 PM
Ishmum Tahzeeb
Ishmum Tahzeeb - avatar
0
https://code.sololearn.com/clL46z1HnD25/?ref=app hope that helps. Just a suggestion, you should try yourself also
2nd Dec 2019, 5:42 PM
XXX
XXX - avatar
- 1
It works for me. n is the number of terms in the series (including the initial '2'). a(n) is the n'th term, s(n) is the sum of the first n terms. The formula I gave simplifies to: 4n^2-2n. n=1: 2 = 2. s(n)=4(1^2)-2(1) = 4-2= 2. n=2: 2 + 10 = 12. s(n)=4(2^2)-2(2)= 16 - 4 =12. n=3: 2 + 10 + 18 = 30. s(n)=4(3^2)-2(3)=4(9)-6=30. n=4: 2+10+18+26=56. s(n)=4(4^2)-2(4)=56. more generally, if you assume s(n) = 4n^2 - 2n, and we know that that the nth term a(n) = 2 + 8(n-1), you can show that s(n+1)=s(n)+a(n+1), so qed by induction.
3rd Dec 2019, 9:17 PM
Hope J
- 1
Hope J I just tried your formula. Now that I am looking more closely at it I can see that it doesn't work because you were mixing up the variables as they were given in the task. There n is the last number in the series and not the count of numbers in the series.
3rd Dec 2019, 9:41 PM
Thoq!
Thoq! - avatar
- 1
Ok right. I think the right formula then is s(n) = [(n + 4)^2 - 4]/16. I only verified up to n=26 (four terms) .
3rd Dec 2019, 10:04 PM
Hope J