-3
$\begingroup$

Having written the following program in Python:

#Collatz Conjecture
#if n is odd, 3n+1
#if n is even, n/2
while True:
    print("**********")
    n=input("Which number would you like to test? ")
    i=1
    print("**********")
    while n!=1:
        print("Step " + str(i) + ": " + str(n))
        n=float(n)
        if(n%2==1):
            n=(n*3)+1
        elif(n%2==0):
            n=n/2
        else:
            print("Invalid number.")
        i+=1

When given the number: 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

The second step is the result inf - does this break the Collatz Conjecture, or not?

  • 3
    I'd say that what you broke is Python :) I mean, what is the biggest number that python can store in a regular variable?2017-01-06
  • 0
    USe Pari/GP and try integers up to 15 000 digits and more...2017-01-06
  • 0
    Nathan, you can remove the elif as if its not even its odd that will save you some time2017-08-02

1 Answers 1

4

This is an artifact of programming; in particular, you got that result because the number provided is larger than $2^{128}$, hence a single-precision floating point number cannot represent it and rounds it to infinity. Obviously, it it is not possible for the Collatz iteration to ever reach the value infinity.

Also, as a note, I don't know why you have the line n = (float)n, but such a cast is a really bad idea, since you are dealing with exact computation in integers, and this line will cause rounding issues for larger integers. Rather, you need to use an arbitrarily sized integer type for this sort of computation.

  • 0
    Thanks, I had a feeling it was Python that was broken, and not the conjecture! Upvoting!2017-01-06