Question: Collatz conjecture Consider the following procedure: 1. Start with an integer x. 2. If x is even, then divide it by 2; if it's odd,

Collatz conjecture

Consider the following procedure:

1. Start with an integer x.

2. If x is even, then divide it by 2; if it's odd, instead multiply it by 3 and add 1.

3. Repeat this process until x = 1.

For example, starting with x=15 would give the sequence of numbers 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1. Write a program that has the user enter a value for x and then displays the resulting sequence. To check whether a number is even you should use the % operator: if x is even, then x % 2 == 0 and if it's odd x % 2 == 1.

Extra note from my professor: (Background: the Collatz conjecture is that regardless of what number you start with, you will eventually reach 1 through this procedure. Although it was formulated about 80 years ago, nobody has yet managed to prove or disprove it it has been checked by computer up to x=6,000,000,000,000,000,000 and so far every one of them has had this behavior, but it is still not known whether it's true for all integers. If you happen to find an x that doesn't go to 1, you could win some notoriety!)

This is a beginner course, so the program can be kept fairly simple. I have no experience with programming, so most stuff is all new to me. The main topics of our lectures/chapter this week were Indefinite (while) loops, File loops, Nested loops, Boolean logic, Nesting if statements and loops.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!