pythonmodulocollatz

Code to simulate Collatz Conjecture is showing 1 as an even number


My code is the following

output_even = "Even: "
output_odd = "Odd: "

if varnum % 2 == 0:
 varnum /= 2
 print(output_even, varnum)
 time.sleep(0.1)
elif varnum % 2 != 0:
 varnum *= 3
 varnum += 1
 print(output_odd, varnum)
 time.sleep(0.1)

Output (integer 5):

Odd: 16

Even: 8.0

Even: 4.0

Even: 2.0

Even: 1.0

Conjecture not solved

I know that one should not be an even number. But for some reason, it is listed as an even number as shown by the output with the original number being 5.

Edit: Full loop with code for extra clarification hopefully

        while varnum != 1:
            if varnum % 2 == 0:
                print(output_even, varnum)
                varnum /= 2
                time.sleep(0.1)
            elif varnum % 2 != 0:
                print(output_odd, varnum)
                varnum *= 3
                varnum += 1
                time.sleep(0.1)
        if varnum == 1:
            print("Conjecture not solved")
        else:
            print("Conjecture solved")

Solution

  • 2 is even, though.

    You're dividing varnum by 2 and then printing the result of that division.

    Try swapping the arithmetic and printing operations.