Collatz Sequence & Binary Analysis
Project Overview
This project explores algorithm implementation at the hardware architecture level using MIPS Assembly. It calculates the famous Collatz Sequence (also known as the $3n+1$ problem) for any given integer.
Beyond generating the sequence, the program performs a deep statistical analysis: for every number generated, it inspects its raw binary representation to calculate its Hamming Weight (the count of set bits / 1s), identifying which number in the sequence is computationally “densest”.
Multi-Language Support 🇮🇹 🇬🇧
The logic and documentation are available in both languages:
- English:
Project_eng.txt - Italian:
Project_ita.txt
The Algorithm
The core loop implements the Collatz rules using efficient bitwise logic instead of expensive division/multiplication instructions where possible:
- Input Handling: Accepts integers, automatically converting negative inputs to positive absolute values.
- Sequence Logic:
- If Even: Divide by 2 (Implemented via logical right shift
srlor arithmetic logic). - If Odd: Multiply by 3 and add 1.
- Termination: The loop halts when the sequence reaches
1.
- If Even: Divide by 2 (Implemented via logical right shift
Bitwise Analysis (Hamming Weight)
The unique feature of this implementation is the Binary Analysis module. For each number $X$ in the sequence, the program runs an inner loop that:
- Iterates through all 32 bits of the register.
- Uses masking (
andi) and shifting (srl) to count how many bits are set to1. - Compares the count with the current maximum (
$s3) and updates the record holder ($s2) if necessary.
Example Output
Enter a number: 6
Sequence: 6 3 10 5 16 8 4 2 1
Number of elements in the sequence: 9
Number with the maximum amount of ones in its binary representation: 5
It has a number of ones equal to: 2 (Binary: 101)