3.1.2.15 LAB: Collatz's hypothesis
LAB
Estimated time
20 minutes
Level of difficulty
Medium
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
asc0 ÷ 2
; - otherwise, if it's odd, evaluate a new
c0
as3 × 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
#Reading the natural Number
ReplyDeleten = int(input("Enter the Natural Number : "))
# We simply follow steps
# while we do not reach 1
count = 0
while n != 1:
# If n is odd
if n & 1:
n = 3 * n + 1
# If even
else:
n = n // 2
count += 1
print(n)
# Print 1 at the end
print(n)
#Displaying Counted Steps
print("steps = ",count)
thanks didi
Deletethis Annonymus is me Atri upadhayay
Deletenum=int(input("enter the natural number"))
ReplyDeletecount=0
while num!=1:
if num%2:
num = 3*num +1
else:
num = num //2
count +=1
print(num)
print("step",count)