2016 September 14,

CS 111: Something Interesting

Now we're going to do something interesting. And by "interesting" I mean, "No human being has ever fully understood the thing that we're about to do."

I have a friend named Paul with a strange quirk. When he hears a positive integer, he responds in one of two ways. If the integer is even, then he divides it in two and speaks the answer aloud. If the integer is odd, then he multiplies it by three, adds one, and speaks the answer aloud. For example, he responds to "14" with "7", and he responds to "15" with "46". Paul is very good at mental arithmetic; he responds to "1729" with "5188" rapidly.

Question 11A: Write a program that uses an if statement to print Paul's response for any given number. Hint: To test whether an integer is even, test whether it has a remainder of 0 when divided by 2.

The problem with Paul is that he hears to his own numbers. If you start him at 15, then he will say 46, then 23, then 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 2, 1.

Question 11B: What happens after Paul reaches 1?

Question 11C: Using a while loop and an if statement, write a program that starts with any given number and prints Paul's responses until he reaches 1. I'll get you started:

number = 5
print(number)
while number > 1:

Be sure to test your code on some small numbers, to make sure it works. Also run your code on the numbers 26 and 27. Although there's not much difference between these two numbers, Paul's monologue based on 27 takes about 10 times as long as his monologue based on 26.

Question 11D: Modify your code from the previous question, to accomplish this new task: Write a program that starts with any given number, and prints out how many numbers Paul says, in his monologue based on that number. For example, if the starting number is 5, then your program should print 6 (because he says "5", "16", "8", "4", "2", "1").

For the sake of brevity, let's call the number you just computed the "monologue length". For example, the monologue length of 5 is 6, because he says 6 numbers in his monologue that starts at 5. The monologue length of 16 is 5.

Question 11E: Write a program that prints out the monologue length of every integer from 1 to 100.

Does every one of these monologues end? Or are there some starting numbers — maybe very large ones — that lead to never-ending monologues? The amazing reality is: Nobody knows. It's a famous unsolved problem in mathematics.