solutions1/collatz.scala
changeset 208 f8883f8a14ad
child 266 ca48ac1d3c3e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/solutions1/collatz.scala	Sat Nov 17 13:35:08 2018 +0000
@@ -0,0 +1,29 @@
+// Part 1 about the 3n+1 conjecture
+//==================================
+
+
+object CW6a { // for purposes of generating a jar
+
+def collatz(n: Long): Long =
+  if (n == 1) 0 else
+    if (n % 2 == 0) 1 + collatz(n / 2) else 
+      1 + collatz(3 * n + 1)
+
+
+def collatz_max(bnd: Long): (Long, Long) = {
+  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
+  all.maxBy(_._1)
+}
+
+
+/* some test cases
+val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
+
+for (bnd <- bnds) {
+  val (steps, max) = collatz_max(bnd)
+  println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
+}
+
+*/
+
+}