A Go implementation to validate the Collatz conjecture, which states that for any positive integer:
- If it's even, divide it by 2
- If it's odd, multiply it by 3 and add 1
This process will always eventually reach 1.
To read more about this conjecture, please check Wikipedia.
- ✨ Support for arbitrarily large numbers using
math/big - 🎯 Command-line arguments for customization
- 📊 Step counter and maximum value tracking
- ⏱️ Performance timing
- 🔍 Optional verbose mode to see each step
- 🎨 Clean, idiomatic Go code
Run with default values (314^5):
go run main.goSpecify a custom starting number:
go run main.go -num 27Specify a number and raise it to a power:
go run main.go -num 314 -power 5See each step in the sequence:
go run main.go -num 27 -verboseTest with very large numbers:
go run main.go -num 999999999999999999999999999-num: Starting number (default: "314")-power: Raise the starting number to this power (default: 5)-verbose: Enable verbose output showing each step (default: false)
Starting number: 27
✓ Reached 1 after 111 steps
Maximum value reached: 9232
Time elapsed: 15.583µs
With verbose mode:
Starting number: 27
Step 1: 27 (odd) -> 82
Step 2: 82 (even) -> 41
Step 3: 41 (odd) -> 124
...
✓ Reached 1 after 111 steps
Maximum value reached: 9232
Time elapsed: 89.333µs
Build the executable:
go build -o collatzRun the executable:
./collatz -num 100 -verbose