progs/collatz_sol.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 08 Nov 2016 10:37:18 +0000
changeset 17 ecf83084e41d
parent 15 52713e632ac0
child 18 87e55eb309ed
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
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    27
//     steps. You should expect bounds in the range of 1
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    28
//     upto 10 million. 
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    29
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
def collatz_max(bnd: Int): Int = {
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  (for (i <- 1 to bnd) yield collatz(i).length).max
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
}
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
17
ecf83084e41d updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    35
// some testing harness
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
val bnds = List(10, 100, 1000, 10000, 100000, 1000000, 10000000)
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
for (bnd <- bnds) {
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  val max = collatz_max(bnd)
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  println(s"In the range of 1 - ${bnd} the maximum steps are ${max}")
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
}
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42