Skip to content

rehabaam/3xplus1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Collatz Conjecture (3x+1)

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.

Features

  • ✨ 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

Usage

Basic Usage

Run with default values (314^5):

go run main.go

Custom Starting Number

Specify a custom starting number:

go run main.go -num 27

Use Power

Specify a number and raise it to a power:

go run main.go -num 314 -power 5

Verbose Mode

See each step in the sequence:

go run main.go -num 27 -verbose

Large Numbers

Test with very large numbers:

go run main.go -num 999999999999999999999999999

Command-Line Flags

  • -num : Starting number (default: "314")
  • -power : Raise the starting number to this power (default: 5)
  • -verbose : Enable verbose output showing each step (default: false)

Example Output

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

Building

Build the executable:

go build -o collatz

Run the executable:

./collatz -num 100 -verbose

About

Collatz conjecture (known as 3x+1)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages