How to fix it: change the division operators (/
) to floor division operators (//
).
N=int(input())
a=((1+N)*1000)
a=int(a)
s=((a//2)*N//1000)
s=int(s)
print((a//2),N)
print(s%N)
Here's the same code with better formatting and the explicit int
type conversions combined into the variable definition:
N = int(input())
a = (1 + N) * 1000
s = (a // 2) * N // 1000
print((a // 2), N)
print(s % N)
Why this works: Standard division in Python will convert the dividend and the divisor to floating point numbers, resulting in a quotient that is also a floating point number. Floor division will not convert the dividend or the divisor, and will result in an integer. For more reading on this issue, check out Floating Point Arithmetic: Issues and Limitations on the Python documentation