The sum of the sum of algebraic progression

Question:

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)

It’s simple algebraic progression sum, then 2 ≤ N ≤ 10^100

if N=1000000000 (10^9) after programme I have: s=500000000500000000 – it’s normal, but if I have N>10^9 for example
N=1000000000000 (10^10) s=50000000005000003584 etc.

How to fix it?)
What gives 3584

Asked By: Zen4

||

Answers:

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

Answered By: David
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.