Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren+ 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))
10 Antworten
+ 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
+ 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)
+ 5
https://www.sololearn.com/discuss/1217474/?ref=app
https://www.sololearn.com/discuss/2699352/?ref=app
https://www.sololearn.com/discuss/1707753/?ref=app
https://www.sololearn.com/discuss/2724844/?ref=app
https://www.sololearn.com/discuss/2539838/?ref=app
https://www.sololearn.com/discuss/939556/?ref=app
https://www.sololearn.com/discuss/342395/?ref=app
+ 4
Frogged what a great library!!!
+ 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
+ 3
Lisa 's code is shortest and understandable.
n = int(input())
print(sum(range(n+1)))
+ 2
from the interval [-25, 25] to derive all the numbers, which when divided by 3 give the remainder 2
+ 2
print(*filter(lambda x: x % 3 == 2, range(-25, 26)))
- 1
N = int(input())
count = N
x = range(1, N +1)
for i in x:
N = i + N
print(N - count)