I want to make a code that gives me the sum of consecutive numbers starting from 1 to any number say N | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

I want to make a code that gives me the sum of consecutive numbers starting from 1 to any number say N

Is there another way to do this? I've done the following code with mathematical formula...can anyone tell other way for doing this on python? N = int(input()) print(int(N*(N+1)/2))

7th Apr 2021, 4:49 PM
Ayushman Halder
Ayushman Halder - avatar
10 Answers
+ 8
Two different ways N = int(input()) x = range(1,N+1) for i in x : N = N + i print(N) N= int(input()) i = sum(range(1,N+1)) print(i) Good luck
8th Apr 2021, 10:36 AM
Ravindu Dilshan
Ravindu Dilshan - avatar
+ 6
Create a list with the numbers upto N..... list=[1,2,3,....,N] sum=0 for i in list: sum=sum+i print(sum)
7th Apr 2021, 5:01 PM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 4
Frogged what a great library!!!
7th Apr 2021, 10:39 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 3
sum(range(1, N+1)) if built-in function are allowed range creates an integer list with n element of [1, N], sum sums up all elements of this list
7th Apr 2021, 5:54 PM
Lisa
Lisa - avatar
+ 3
Lisa 's code is shortest and understandable. n = int(input()) print(sum(range(n+1)))
7th Apr 2021, 6:03 PM
Shadoff
Shadoff - avatar
7th Apr 2021, 7:02 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
from the interval [-25, 25] to derive all the numbers, which when divided by 3 give the remainder 2
9th Apr 2021, 12:05 PM
Варя
Варя - avatar
+ 2
print(*filter(lambda x: x % 3 == 2, range(-25, 26)))
10th Apr 2021, 3:27 AM
Per Bratthammar
Per Bratthammar - avatar
- 1
N = int(input()) count = N x = range(1, N +1) for i in x: N = i + N print(N - count)
18th May 2021, 8:08 AM
Ислам Алб
Ислам Алб - avatar