0
I did it in a different way. What is the real method to find the sum of all numbers till the given number?
N = int(input()) print(int((N*(N+1))/2))
3 odpowiedzi
+ 7
You can use list functions or for loop
1. print(sum(list(range(int(input())+1))))
2.
n = int(input())
sum = 0
for i in range(n+1):
sum += i
i+=1
print(sum)
+ 3
The way you have already done it is better than using a loop or the sum() function if you're concerned about your time and space complexity. Your use of the Gauss formula is accomplished in constant time O(1) while the use of a loop is linear time O(N).
+ 1
Thnx i got it.