Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading