progs/live.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 05 Nov 2016 18:07:21 +0000
changeset 9 48a477fdef21
child 10 e7eeeb5b41dc
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import scala.annotation.tailrec   
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
def collatz(n: Int): List[Int] =
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
  if (n == 1) List(1) else
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
    if (n % 2 == 0) (n::collatz(n / 2)) else 
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
      (n::collatz(3 * n + 1))
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
def collatz1(n: Int): Int =
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
  if (n == 1) 1 else
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
    if (n % 2 == 0) (1 + collatz1(n / 2)) else 
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
      (1 + collatz1(3 * n + 1))
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
@tailrec
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
def collatz2(n: BigInt, acc: Int): Int =
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  if (n == 1) acc else
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
    if (n % 2 == 0) collatz2(n / 2, acc + 1) else 
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
      collatz2(3 * n + 1, acc + 1)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
collatz(1)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
collatz(2)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
collatz(3)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
collatz(4)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
collatz(5)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
collatz(6)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
collatz(7)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
collatz(8)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
collatz(9)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
collatz(100000)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
println((for (i <- 1 to 100000) yield collatz(i).length).max)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
println((for (i <- 1 to 100000) yield collatz1(i)).max)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
println((for (i <- 1 to 1000000) yield collatz2(i, 1)).max)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
println((for (i <- (1 to 10000000).par) yield collatz2(i, 1)).max)
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
println((for (i <- (1 to 100000000).par) yield collatz2(i, 1)).max)