progs/collatz_sol.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 09 Nov 2016 00:52:08 +0000
changeset 18 87e55eb309ed
parent 17 ecf83084e41d
child 24 66b97f9a40f8
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     1
// Part 1 about the 3n+1 conceture
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     2
//=================================
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
17
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
     4
//(1) Complete the collatz function below. It should
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
     5
//    recursively calculate the number of steps needed 
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
     6
//    until the collatz series reaches the number 1.
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
     7
//    If needed you can use an auxilary function that
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
     8
//    performs the recursion. The function should expect
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
     9
//    arguments in the range of 1 to 10 Million.
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
17
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    11
def collatz(n: Long): List[Long] =
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    12
  if (n == 1) List(1) else
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    13
    if (n % 2 == 0) (n::collatz(n / 2)) else 
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    14
      (n::collatz(3 * n + 1))
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    15
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
// an alternative that calculates the steps directly
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
def collatz1(n: Long): Int =
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  if (n == 1) 1 else
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    if (n % 2 == 0) (1 + collatz1(n / 2)) else 
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
      (1 + collatz1(3 * n + 1))
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
17
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    24
//(2)  Complete the collatz bound function below. It should
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    25
//     calculuate how many steps are needed for each number 
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    26
//     from 1 upto a bound and return the maximum number of
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    27
//     steps and the corresponding number that needs that many 
17
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    28
//     steps. You should expect bounds in the range of 1
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    29
//     upto 1 million. 
17
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    30
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    31
def collatz_max(bnd: Int): (Int, Int) = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    32
  val all = for (i <- (1 to bnd).toList) yield collatz(i).length
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    33
  val max = all.max
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    34
  (all.indexOf(max) + 1, max)
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
}
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
17
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    38
// some testing harness
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    39
val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
for (bnd <- bnds) {
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    42
  val (max, steps) = collatz_max(bnd)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    43
  println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
}
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    46
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    47
//val all = for (i <- (1 to 100000).toList) yield collatz1(i)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 17
diff changeset
    48
//println(all.sorted.reverse.take(10))