0

Using // and / in the following code (location marked in-code), I get different answers:

x = 2**(221) + 1

count = 0

while x != 1:
    if x%2 == 0: 
        x = x//2 # HERE: In this line - case (1) x = x//2, case(2) x = x/2
        count += 1
    elif x%2 == 1:
        x = 3*x + 1
        count += 1
        
count

In case (1), count = 1754. In case (2), count = 229. Why are the outputs different??

algebroo
  • 132
  • 6
  • Floating point numbers of that size have no accuracy anymore. – Klaus D. Jul 31 '21 at 06:41
  • Regular division always produces a float, even if the result could be an integer. – Barmar Jul 31 '21 at 06:44
  • Also note that `2**(221) + 1` will be represented exact using Python's integers (they are unbounded), while floating point values will be restricted to 64 bit approximations. In Python3, a division with a single `/` will always result in a floating point value. – JohanC Aug 01 '21 at 23:48

0 Answers0