I used the Gauss summation method for ADDNATRL (Add Natural Numbers). I think the math itself is fine. To my understanding, long was discontinued in Python 3, so I have used type int in my solution. Test cases work as expected, including the maximum N = 10^9, but when I submitted my solution, it was marked as incorrect. I am not sure what I am doing wrong. Any advice would be greatly appreciated.
Try to plug in N = 134217729.
If you do: (134217729 * 134217730) / 2 in a Calculator you get:
9007199456067585
but your code returns:
9007199456067584
How to fix?
The correct answer is:
N = int(input())
print(N * (N+1)//2)
Why?
If you do 6/3, python converts the result to float, which is an approximation for a real number: here 3.0. Since it is an approximation, there can be slight errors if your numbers get large, which is why you get your error.
Instead use 6//3 which is integer division: Then your numbers don’t get converted and your answer will be correct.