Showing TLE for the following program,solution?

n = int(input())
i = 1
sum = 0
while(i<n+1) :
sum = sum + i
i = i + 1
print(sum)

First of all u should always format your code properly before posting and give the problem link for which you are asking for help.
If it is what i think it is (sum of first n numbers) then there is a formula for it which gives answer in constant time
The formula is n*(n+1)/2

1 Like