⚙️
Collatz Conjecture Iterator
This code snippet is a simple implementation of an iterator that generates the Collatz sequence for a given positive integer greater than 1. The Co…
This code snippet is a simple implementation of an iterator that generates the Collatz sequence for a given positive integer greater than 1. The Co…
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:
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.
To generate a Collatz Sequence,
Please enter a positive integer number: 6
3 10 5 16 8 4 2 1
python collatz.py