|
1 | | -print("=" * 50) |
2 | | -print("THE COLLATZ CONJECTURE SEQUENCE") |
3 | | -print("=" * 50) |
4 | | -print("\nAlso known as the 3n+1 problem") |
5 | | -print("Rules:") |
6 | | -print("- If the number is even: divide by 2") |
7 | | -print("- If the number is odd: multiply by 3 and add 1") |
8 | | -print("- Continue until you reach 1") |
9 | | -print("=" * 50) |
| 1 | +print("🎮 The Collatz Conjecture Sequence 🎮") |
| 2 | +print("Also known as the 3n+1 problem\n") |
| 3 | +print("📚 Rules:") |
| 4 | +print(" - If the number is even: divide by 2") |
| 5 | +print(" - If the number is odd: multiply by 3 and add 1") |
| 6 | +print(" - Continue until you reach 1\n") |
10 | 7 |
|
11 | 8 | MAX_STEPS = 100000 |
12 | | -MAX_VALUE = 10**18 |
13 | 9 | MAX_INPUT = 10**12 |
14 | | -steps_cache = {1: 0} |
15 | | - |
16 | | -def collatz_next(n): |
17 | | - return n // 2 if n % 2 == 0 else 3 * n + 1 |
18 | | - |
19 | | -def get_remaining_sequence(n): |
20 | | - seq = [] |
21 | | - while n != 1: |
22 | | - n = collatz_next(n) |
23 | | - seq.append(n) |
24 | | - return seq |
25 | | -def collatz_sequence(start): |
26 | | - if start in steps_cache: |
27 | | - n = start |
28 | | - yield n |
29 | | - while n != 1: |
30 | | - n = collatz_next(n) |
31 | | - yield n |
32 | | - return |
33 | | - |
34 | | - n = start |
35 | | - path = [n] |
36 | | - steps = 0 |
37 | | - |
38 | | - yield n |
39 | | - |
40 | | - while n != 1 and steps < MAX_STEPS: |
41 | | - n = collatz_next(n) |
42 | | - steps += 1 |
43 | | - |
44 | | - if n > MAX_VALUE: |
45 | | - print("\n\n⚠ Computation stopped: safety limit reached") |
46 | | - break |
47 | | - |
48 | | - yield n |
49 | | - path.append(n) |
50 | | - |
51 | | - if n in steps_cache: |
52 | | - for remaining in get_remaining_sequence(n): |
53 | | - yield remaining |
54 | | - steps += steps_cache[n] |
55 | | - break |
56 | | - |
57 | | - total = steps_cache.get(n, 0) |
58 | | - for value in reversed(path): |
59 | | - total += 1 |
60 | | - steps_cache[value] = total |
61 | 10 |
|
62 | 11 | while True: |
63 | 12 | try: |
64 | | - number = int(input("\nEnter a positive integer to start: ")) |
65 | | - if number > 0: |
66 | | - if number > MAX_INPUT: |
67 | | - print(f"Input too large! Maximum allowed: {MAX_INPUT:,}") |
68 | | - continue |
69 | | - break |
70 | | - else: |
71 | | - print("Please enter a positive integer!") |
| 13 | + number = int(input("🎯 Enter a positive integer to start: ")) |
| 14 | + if number <= 0: |
| 15 | + print("❌ Please enter a positive integer!") |
| 16 | + continue |
| 17 | + if number > MAX_INPUT: |
| 18 | + print(f"⚠️ Input too large! Maximum allowed: {MAX_INPUT:,}") |
| 19 | + continue |
72 | 20 | except ValueError: |
73 | | - print("Please enter a valid number!") |
74 | | - |
75 | | -original_number = number |
76 | | - |
77 | | -print(f"\nStarting with: {number}") |
78 | | -print("\nSequence:") |
79 | | - |
80 | | -sequence = [] |
81 | | -steps = 0 |
82 | | -max_value = number |
83 | | - |
84 | | -gen = collatz_sequence(number) |
| 21 | + print("❌ Please enter a valid number!") |
| 22 | + continue |
85 | 23 |
|
86 | | -first = next(gen) |
87 | | -print(first, end="") |
88 | | -sequence.append(first) |
89 | | - |
90 | | -for num in gen: |
91 | | - sequence.append(num) |
92 | | - steps += 1 |
93 | | - max_value = max(max_value, num) |
94 | | - print(f" → {num}", end="") |
95 | | - if steps % 10 == 0: |
96 | | - print() |
97 | | - |
98 | | -print("\n\n" + "=" * 50) |
99 | | -print("SEQUENCE COMPLETE!") |
100 | | -print("=" * 50) |
101 | | -print(f"Starting number: {original_number}") |
102 | | -print(f"Total steps: {steps}") |
103 | | -print(f"Sequence length: {len(sequence)}") |
104 | | -print(f"Highest number reached: {max_value}") |
| 24 | + original_number = number |
| 25 | + sequence = [number] |
| 26 | + steps = 0 |
| 27 | + max_value = number |
105 | 28 |
|
106 | | -if len(sequence) <= 100: |
107 | | - view_details = input("\nWould you like to see step-by-step details? (yes/no): ").lower() |
108 | | - if view_details in ['yes', 'y']: |
109 | | - print("\nDetailed Steps:") |
110 | | - print("-" * 50) |
111 | | - for i in range(len(sequence) - 1): |
112 | | - current = sequence[i] |
113 | | - next_num = sequence[i + 1] |
114 | | - if current % 2 == 0: |
115 | | - print(f"Step {i + 1}: {current} is even → {current} ÷ 2 = {next_num}") |
116 | | - else: |
117 | | - print(f"Step {i + 1}: {current} is odd → ({current} × 3) + 1 = {next_num}") |
118 | | - print("-" * 50) |
| 29 | + print(f"\n🚀 Starting with: {number}") |
| 30 | + print("📊 Sequence:") |
| 31 | + print(number, end="") |
119 | 32 |
|
120 | | -print("\n✓ The sequence reached 1 as expected!") |
121 | | -print("=" * 50) |
| 33 | + while number != 1 and steps < MAX_STEPS: |
| 34 | + if number % 2 == 0: |
| 35 | + number = number // 2 |
| 36 | + else: |
| 37 | + number = 3 * number + 1 |
| 38 | + |
| 39 | + steps += 1 |
| 40 | + sequence.append(number) |
| 41 | + |
| 42 | + if number > max_value: |
| 43 | + max_value = number |
| 44 | + |
| 45 | + print(f" ➡️ {number}", end="") |
| 46 | + if steps % 10 == 0: |
| 47 | + print() |
| 48 | + |
| 49 | + print("\n\n✅ SEQUENCE COMPLETE!") |
| 50 | + print(f"📍 Starting number: {original_number}") |
| 51 | + print(f"👣 Total steps: {steps}") |
| 52 | + print(f"📏 Sequence length: {len(sequence)}") |
| 53 | + print(f"🏆 Highest number reached: {max_value}") |
| 54 | + |
| 55 | + if len(sequence) <= 100: |
| 56 | + view_details = input("\n🔍 Would you like to see step-by-step details? (y/n): ").strip().lower() |
| 57 | + if view_details in ['y', 'yes']: |
| 58 | + print("\n📝 Detailed Steps:") |
| 59 | + for i in range(len(sequence) - 1): |
| 60 | + current = sequence[i] |
| 61 | + next_num = sequence[i + 1] |
| 62 | + if current % 2 == 0: |
| 63 | + print(f" Step {i + 1}: {current} is even ➡️ {current} ÷ 2 = {next_num}") |
| 64 | + else: |
| 65 | + print(f" Step {i + 1}: {current} is odd ➡️ ({current} × 3) + 1 = {next_num}") |
| 66 | + |
| 67 | + print("\n🎉 The sequence reached 1 as expected!") |
| 68 | + |
| 69 | + again = input("\n🔄 Do you want to test another number? (y/n): ").strip().lower() |
| 70 | + if again != 'y': |
| 71 | + print("\n👋 Thanks for exploring the Collatz Conjecture! Goodbye!\n") |
| 72 | + break |
0 commit comments