var | desc |
---|---|
Positive integer |
This algorithm takes a positive integer and applies one of two operations based on the following conditionals:
-
If
$n$ is even: Divide$n$ by two:$n \div 2$ -
If
$n$ is odd: Multiply$n$ by$3$ and add$1$ :$n \times 3 + 1$
This assessment can be solved by using a simple if-else condition.
We assure if
If this equation is true then we know
We repeat this process until
In Rust 🦀 code:
fn main() {
let mut inp: u64 = std::io::read_to_string(std::io::stdin())
.unwrap()
.trim()
.parse()
.unwrap();
print!("{inp}");
while inp > 1 {
if inp % 2 == 0 { inp /= 2; }
else { inp = inp * 3 + 1; }
print!(" {inp}");
}
println!();
}