updated
authorChristian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 05 Nov 2016 18:15:38 +0000
changeset 10 e7eeeb5b41dc
parent 9 48a477fdef21
child 11 417869f65585
updated
progs/live.scala
--- a/progs/live.scala	Sat Nov 05 18:07:21 2016 +0000
+++ b/progs/live.scala	Sat Nov 05 18:15:38 2016 +0000
@@ -12,11 +12,17 @@
       (1 + collatz1(3 * n + 1))
 
 @tailrec
-def collatz2(n: BigInt, acc: Int): Int =
+def collatz2(n: Long, acc: Int): Int =
   if (n == 1) acc else
     if (n % 2 == 0) collatz2(n / 2, acc + 1) else 
       collatz2(3 * n + 1, acc + 1)
 
+@tailrec
+def collatz3(n: BigInt, acc: Int): Int =
+  if (n == 1) acc else
+    if (n % 2 == 0) collatz3(n / 2, acc + 1) else 
+      collatz3(3 * n + 1, acc + 1)
+
 collatz(1)
 collatz(2)
 collatz(3)
@@ -31,4 +37,4 @@
 println((for (i <- 1 to 100000) yield collatz1(i)).max)
 println((for (i <- 1 to 1000000) yield collatz2(i, 1)).max)
 println((for (i <- (1 to 10000000).par) yield collatz2(i, 1)).max)
-println((for (i <- (1 to 100000000).par) yield collatz2(i, 1)).max)
+println((for (i <- (1 to 100000000).par) yield collatz3(i, 1)).max)