Search code examples
pythonpython-3.x

Python 3 gives wrong output when dividing large whole numbers?


a = 15511210043330985984000000  # (25!)
b = 479001600                   # (12!)
c = 6227020800                  # (13!)

On dividing ans = int(a/(b*c)) or ans = int((a/b)/c) we get ans equal to 5200299 instead of 5200300


Solution

  • In Python 3.x / means floating point division and can give small rounding errors. Use // for integer division.

    ans = a // (b*c)