progs/live.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 22 Jan 2019 12:53:05 +0000
changeset 258 ebe71908b13e
parent 11 417869f65585
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
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 10
diff changeset
     4
def collatz(n: Long): List[Long] =
9
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
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 10
diff changeset
     9
def collatz1(n: Long): Int =
9
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
10
e7eeeb5b41dc updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 9
diff changeset
    15
def collatz2(n: Long, acc: Int): Int =
9
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)
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 10
diff changeset
    25
collatz(6).length
9
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)
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 10
diff changeset
    28
collatz(9).length
9
48a477fdef21 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
collatz(100000)
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 10
diff changeset
    30
println((for (i <- 1 to 10000000) yield collatz(i).length).max)
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 10
diff changeset
    31
println((for (i <- 1 to 10000000) yield collatz1(i)).max)
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 10
diff changeset
    32
println((for (i <- 1 to 10000000) yield collatz2(i, 1)).max)
9
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)
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 10
diff changeset
    34