-->

Python 3.2.1.15 LAB: Collatz's hypothesis

 3.2.1.15 LAB: Collatz's hypothesis

Collatz's hypothesis



If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.2.1.15 LAB: Collatz's hypothesis.

Objectives

Familiarize the student with:

  • using the while loop;
  • converting verbally defined loops into actual Python code.

Scenario

In 1937, a German mathematician named Lothar Collatz formulated an intriguing hypothesis (it still remains unproven) which can be described in the following way:

take any non-negative and non-zero integer number and name it c0;

if it's even, evaluate a new c0 as c0 ÷ 2;

otherwise, if it's odd, evaluate a new c0 as 3 × c0 + 1;

if c0 ≠ 1, skip to point 2.

The hypothesis says that regardless of the initial value of c0, it will always go to 1.

Of course, it's an extremely complex task to use a computer in order to prove the hypothesis for any natural number (it may even require artificial intelligence), but you can use Python to check some individual numbers. Maybe you'll even find the one which would disprove the hypothesis.

Write a program which reads one natural number and executes the above steps as long as c0 remains different from 1. We also want you to count the steps needed to achieve the goal. Your code should output all the intermediate values of c0, too.

Hint: the most important part of the problem is how to transform Collatz's idea into a while loop - this is the key to success.

Test your code using the data we've provided.

Test Data

Sample input: 15

Expected output:

  • 46
  • 23
  • 70
  • 35
  • 106
  • 53
  • 160
  • 80
  • 40
  • 20
  • 10
  • 5
  • 16
  • 8
  • 4
  • 2
  • 1
  • steps = 17

Sample input: 16

Expected output:

  • 8
  • 4
  • 2
  • 1

steps = 4

Sample input: 1023

Expected output:

  • 3070
  • 1535
  • 4606
  • 2303
  • 6910
  • 3455
  • 10366
  • 5183
  • 15550
  • 7775
  • 23326
  • 11663
  • 34990
  • 17495
  • 52486
  • 26243
  • 78730
  • 39365
  • 118096
  • 59048
  • 29524
  • 14762
  • 7381
  • 22144
  • 11072
  • 5536
  • 2768
  • 1384
  • 692
  • 346
  • 173
  • 520
  • 260
  • 130
  • 65
  • 196
  • 98
  • 49
  • 148
  • 74
  • 37
  • 112
  • 56
  • 28
  • 14
  • 7
  • 22
  • 11
  • 34
  • 17
  • 52
  • 26
  • 13
  • 40
  • 20
  • 10
  • 5
  • 16
  • 8
  • 4
  • 2
  • 1

steps = 62

Solution Code

c0 = int(input(" Give me a number: "))
steps = 0
#c0 %2 == 0 means "even number" // c0 %2 == 1 "Odd number"
while c0 != 1:
    if c0 % 2 == 0:   # "even number"
        c0 = int(c0 / 2)
        print(c0)
    else:              # "Odd number"
        c0 = int(3 * c0 + 1)
        print(c0)
    steps += 1
print(f"steps = {steps}")

In below live analysis of the steps, we will notice the following:

  • We added the variable steps to equal 0 so we can add 1 to it in each running loop (iteration)
  • Any number % (modulo) 2 results in 0 means "Even Number"
  • Any number % (modulo) 2 results in 1 means "Odd Number"
  • We started a while loop with the condition that c0 != 1 as the loop will stop after we reach c0 ==1
  • The, inside while loop, we started if block and else block
  • If block will run when we have an Even Number
  • Else block will run when we have an Odd number
  • Whenever we revert back to while loop, steps will be raised by 1 (+=1)
  • Once c0 equals 1, we reach to the final loop which is the last "step". After this loop, we move on to the final code OUTSIDE the while loop (as while loop already ended)


 =================================================================================

Follow the Python page on the Blog to be the first to know

Post a Comment

0 Comments