pyBegin / projects / collatz-conjecture
⚙️

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…

35 linesRunnable

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 Collatz sequence is a process where each number transforms based on the following rules:

Overview

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.

Usage

  1. When executed, the script prompts the user for a positive integer.
  2. The script then iterates through the Collatz sequence, printing each number in the sequence.
  3. The sequence ends when the iterator reaches 1, triggering the StopIteration exception.
  4. If an invalid input is entered (like a non-integer), a ValueError is raised, prompting the user with an error message.

How to Run

Error Handling

Pyodide-runnable

Yes — it is a pure-stdlib console program; the iterator and input/print work unchanged in the browser.