This project demonstrates the Collatz Conjecture, also known as the 3n + 1 Conjecture, using a Rust program. The conjecture is defined as follows:
- Take any positive integer n.
- If n is even, divide it by 2.
- If n is odd, multiply it by 3 and add 1.
- Repeat the process indefinitely.
The conjecture states that no matter what value of n you start with, you will always eventually reach 1.
-
Install Rust on your system.
-
Create a new Rust project using Cargo:
cargo new collatz_conjecture
- Change to the new project directory:
cd collatz_conjecture
-
Replace the content of the src/main.rs file with the provided Rust code for the Collatz Conjecture.
-
Build and run the project with the desired initial number as the argument:
cargo run <initial_number>
Replace <initial_number> with a positive integer.
- The program will output each step of the Collatz Conjecture until it reaches 1.
cargo run 6
Output:
Initial number: 6
Step 1: 3
Step 2: 10
Step 3: 5
Step 4: 16
Step 5: 8
Step 6: 4
Step 7: 2
Step 8: 1
This project is licensed under the MIT License.