This repository was archived by the owner on Dec 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollatz.py
More file actions
57 lines (41 loc) · 1.27 KB
/
Copy pathcollatz.py
File metadata and controls
57 lines (41 loc) · 1.27 KB
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
47
48
49
50
51
52
53
54
55
56
57
#! python3
"""Collatz Sequence
Generates numbers for the Collatz sequence, given a starting number.
More info at: https://en.wikipedia.org/wiki/Collatz_conjecture
View this code at https://nostarch.com/big-book-small-python-projects
Tags: tiny, beginner, math
"""
import sys
import time
def main():
print(
"""Collatz Sequence, or, the 3n + 1 Problem
The Collatz sequence is a sequence of numbers produced from a starting
number n, following three rules:
1) If n is even, the next number n is n / 2.
2) If n is odd, the next number n is n * 3 + 1
3) If n is 1, stop. Otherwise, repeat.
It is generally thought, but so far not mathematically proven, that
every starting number eventually terminates at 1.
"""
)
print("Enter a starting number (greater than 0) or QUIT:")
response = input("> ")
if not response.isdecimal() or response == "0":
print("You must enter an integer greater than 0.")
sys.exit()
n = int(response)
print(n, end="", flush=True)
while n != 1:
if n % 2 == 0:
n //= 2
else:
n = 3 * n + 1
print(", " + str(n), end="", flush=True)
time.sleep(0.1)
print()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(130)