# HG changeset patch # User Christian Urban # Date 1478369738 0 # Node ID e7eeeb5b41dc14821fe93c1201103ff4e4e8807a # Parent 48a477fdef2176b85601c8ffa3cc2b56287be55f updated diff -r 48a477fdef21 -r e7eeeb5b41dc 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)