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))

30th May 2021, 7:07 AM
Joel Sebastian Jijo
Joel Sebastian Jijo - avatar
3 Respostas
+ 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)
30th May 2021, 7:22 AM
Simba
Simba - avatar
+ 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).
30th May 2021, 7:51 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thnx i got it.
30th May 2021, 7:33 AM
Joel Sebastian Jijo
Joel Sebastian Jijo - avatar