mirror of
https://github.com/LukasKalbertodt/programmieren-in-rust.git
synced 2024-11-18 02:48:58 +01:00
Add solution 1.2
This commit is contained in:
parent
6a5de018b6
commit
671ec22f10
21
aufgaben/sheet1/sol2/collatz.rs
Executable file
21
aufgaben/sheet1/sol2/collatz.rs
Executable file
@ -0,0 +1,21 @@
|
|||||||
|
//! Aufgabe 1.2: Collatz
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Hardcoding the input value is ugly, but it's fine for now.
|
||||||
|
let mut n = 27;
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
while n > 1 {
|
||||||
|
// We use the feature "everything is an expression" here to slightly
|
||||||
|
// shorten the code, although, when the task was due, this feature was
|
||||||
|
// not yet introduced to the students.
|
||||||
|
n = if n % 2 == 0 {
|
||||||
|
n / 2
|
||||||
|
} else {
|
||||||
|
n * 3 + 1
|
||||||
|
};
|
||||||
|
count += 1;
|
||||||
|
|
||||||
|
println!("{} -> {}", count, n);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user