0

I need help to understand why this piece of code works correctly in python 2 but it doesn't work for python 3.

res = 76460188758730153884232119087179527041998988911761118644170793575
while res > 31:
    print(res%31)
    res = (res - res%31)/ 31

In python 2 the result of res%31 is: 23, 17, 2, 28, 17, 17

In python 3 the result of res%31 is: 23, 4.0, 17.0, 6.0, 21.0, 22.0, 18.0, 14.0

I'm having problems with long numbers in python3, but I can't solve them.

Thanks in advance,

vcarque
  • 1
  • 2
  • In Python 3, division always returns a float. If you want int division, like in Python 2, use ``//`` instead of ``/`` – khelwood Nov 28 '17 at 10:03

2 Answers2

3

You'll need to use // in Python 3.x to get integer as a result for the division. Using / in python3 always return a floating value.

Harsh
  • 389
  • 2
  • 18
1

It's not a matter of big or small numbers but how python handles division as noted by @khelwood in his/her comment. In python3 / will return flaoat (or duoble to be more precise) and if you want to get integer (or long) you need to use //. See more here or here.

gonczor
  • 3,994
  • 1
  • 21
  • 46