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.
python collatz.py
Yes — it is a pure-stdlib console program; the iterator and input/print work unchanged in the browser.