-
Notifications
You must be signed in to change notification settings - Fork 1
/
collatz_sequence.py
46 lines (38 loc) · 1.03 KB
/
collatz_sequence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# collaz_sequence.py
# Demonstates the 'Collatz sequence'
def collatz(num):
"""
Implements the procedure for generating the Collatz sequence.
Parameters
----------
num : int
The integer that starts the sequence.
"""
# CASE: '1' -- end recursion
if (num == 1):
print("Sequence Complete!")
# CASE: not '1'
else:
# CASE) Even -- print and return (num // 2)
if (num % 2 == 0):
new_num = num // 2
# CASE) Odd -- print and return (3 * number +1)
else:
new_num = 3 * num + 1
print(new_num)
return collatz(new_num)
def netrunner():
"""
Driver code of the program.
"""
try:
# Accept user input
integer = int(input("\nEnter an integer: "))
collatz(integer)
except ValueError:
print("Enter a positive or negative whole number.")
netrunner()
except RecursionError:
print("Enter a positive or negative whole number. Zero is neither.")
# Run program
netrunner()