Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 8cdd221

Browse files
Merge pull request #732 from rakinplaban/collatz
Collatz Conjecture project
2 parents eacc708 + 50a3396 commit 8cdd221

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Collatz Conjecture Iterator
2+
3+
This code snippet is a simple implementation of an iterator that generates the Collatz sequence for a given positive integer greater than 1. The Collatz sequence is a process where each number transforms based on the following rules:
4+
5+
- If the number is even, divide it by 2.
6+
- If the number is odd, multiply it by 3 and add 1.
7+
- Continue this process until you reach 1.
8+
9+
### Overview
10+
11+
The provided code defines a Collatz class that initializes with a positive integer n and implements the iterator pattern to generate the Collatz sequence. The `__next__()` method computes the next number in the sequence according to the rules above. It raises a StopIteration exception when the sequence reaches 1.
12+
#### Usage
13+
14+
1. When executed, the script prompts the user for a positive integer.
15+
2. The script then iterates through the Collatz sequence, printing each number in the sequence.
16+
3. The sequence ends when the iterator reaches 1, triggering the StopIteration exception.
17+
4. If an invalid input is entered (like a non-integer), a ValueError is raised, prompting the user with an error message.
18+
19+
#### How to Run
20+
21+
- Run the script in your Python environment.
22+
- In terminal write this command
23+
> python collatz.py
24+
25+
26+
#### Error Handling
27+
28+
- If the input is not a valid integer, a ValueError exception is raised, prompting the user to enter a valid integer.
29+
- If the iterator reaches 1, a StopIteration exception is raised, indicating the end of the sequence.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Defined a class to generate Collatz Sequence
2+
class Collatz:
3+
# Constructor to initialize the number
4+
def __init__(self, n):
5+
self.n = n
6+
7+
# Iteration method
8+
def __iter__(self):
9+
return self
10+
11+
# Next method to generate next number in the sequence
12+
def __next__(self):
13+
if self.n == 1:
14+
# Stop iteration when the number is 1
15+
raise StopIteration
16+
17+
if self.n % 2 == 0:
18+
self.n = self.n // 2
19+
else:
20+
self.n = 3 * self.n + 1
21+
return self.n
22+
23+
24+
# Main function to test the class
25+
if __name__ == '__main__':
26+
print("To generate a Collatz Sequence,")
27+
try:
28+
input_number = int(input("Please enter a positive integer number: "))
29+
for i in Collatz(input_number):
30+
print(i, end=" ")
31+
except StopIteration:
32+
print("Collatz sequence ended!")
33+
except ValueError:
34+
print("Input Should be an Integer!")

0 commit comments

Comments
 (0)