'Returning sum of given integer n. (Range(1,n+1)) in constant time

We get an input of a positive integer n, and I am supposed to return the sum from 1 to n (including n).

We have to complete the task in a constant time. I have a solution, and I thought it is in constant time, but the tests of the site tells me that I fail. Is my solution not constant?

My solution:

n = int(input())
print(int(n*(n+1)/2))


Solution 1:[1]

Your solution is mathematically valid and should run in constant time. I recommend that you use int division // instead of float division, since floats have a limited range that might cause results to be inaccurate.

n = int(input())
print(n*(n+1)//2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 khelwood