solutions1/collatz.scala
changeset 335 7e00d2b13b04
parent 320 cdfb2ce30a3d
child 336 25d9c3b2bc99
--- a/solutions1/collatz.scala	Thu Apr 23 14:49:54 2020 +0100
+++ b/solutions1/collatz.scala	Wed Aug 12 00:56:20 2020 +0100
@@ -31,4 +31,19 @@
 
 */
 
+def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0
+
+def is_hard(n: Long) : Boolean = is_pow(3 * n + 1)
+
+def last_odd(n: Long) : Long = 
+  if (is_hard(n)) n else
+    if (n % 2 == 0) last_odd(n / 2) else 
+      last_odd(3 * n + 1)
+
+
+//for (i <- 1 to 130) println(s"$i: ${last_odd(i)}")
+
 }
+
+
+