The Collatz Conjecture is a famous unsolved problem in mathematics. It involves taking any positive integer and repeatedly applying the following rules:
- If the number is even, divide it by 2.
- If the number is odd, multiply it by 3 and add 1.
This process is repeated for each new result.
The conjecture asserts that, no matter which positive integer you start with, the sequence will always eventually reach 1.
Example:
Starting with 6, the sequence is: 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1
For more details: Wikipedia, MathWorld
This project analyzes the Collatz Conjecture by computing sequences for numbers within a specified range and storing the results in a SQLite database.
- Processes numbers in a given range and calculates their Collatz sequences.
- Stores the sequence steps, step count, and maximum value reached for each number.
- Uses SQLite for lightweight, file-based data storage.
- Automatically clears existing data before every new computation.
The SQLite database contains a single table named collatz with the following columns:
| Column | Type | Description |
|---|---|---|
number |
INTEGER | The original number (Primary Key) |
steps |
TEXT | JSON-encoded list of sequence steps |
step_count |
INTEGER | Number of steps taken to reach 1 |
max_value |
INTEGER | Maximum value reached during the sequence |
- Python 3.6 or higher
- Uses built-in modules:
sqlite3,json,argparse
Run the script with the start and end of the range as positional arguments:
python main.py <start> <end>
<start>: Start of the range (inclusive), positive integer > 0<end>: End of the range (inclusive), positive integer ≥ start
Compute sequences for numbers from 110 to 42,000 (database is cleared automatically at the start):
python main.py 110 42000
Sample output images from the CLI and the collatz_conjecture.db database, generated after running the script:
.
├── main.py # Main script to run computations
├── setup_database.py # Database setup and utility functions
└── README.md # Project documentation
└── collatz_conjecture.db # Database which stores computed results (will be created when the script runs)
- The database is created (if it doesn’t exist) with the proper table.
- Existing data is automatically cleared before each new computation.
- For each number in the specified range:
- The Collatz sequence is computed.
- The number, steps, step count, and max value are stored.
- After the computation, a summary query prints number(s) that reached the highest step count along with their details.
The resulting collatz_conjecture.db file is a standard SQLite database and can be easily explored using tools like DB Browser for SQLite or any other SQLite viewer.
Feel free to open issues or submit pull requests for improvements!
This project is released under the MIT License.

