From 671ec22f10ae454fd2b8dbd3f9398482919a695f Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Tue, 15 Nov 2016 23:37:40 +0100 Subject: [PATCH] Add solution 1.2 --- aufgaben/sheet1/sol2/collatz.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100755 aufgaben/sheet1/sol2/collatz.rs diff --git a/aufgaben/sheet1/sol2/collatz.rs b/aufgaben/sheet1/sol2/collatz.rs new file mode 100755 index 0000000..c1f356a --- /dev/null +++ b/aufgaben/sheet1/sol2/collatz.rs @@ -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); + } +}