pre_testing1/collatz.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 31 Oct 2020 16:47:46 +0000
changeset 345 40657f9a4e4a
parent 323 testing1/collatz.scala@1f8005b4cdf6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     1
object CW6a {
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
     2
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     3
//(1) Complete the collatz function below. It should
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     4
//    recursively calculate the number of steps needed 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     5
//    until the collatz series reaches the number 1.
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     6
//    If needed, you can use an auxiliary function that
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     7
//    performs the recursion. The function should expect
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     8
//    arguments in the range of 1 to 1 Million.
323
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
     9
def stepsCounter(n: Long, s: Long) : Long = n match{
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    10
    case 1 => s
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    11
    case n if(n%2==0) => stepsCounter(n/2,s+1)
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    12
    case _ => stepsCounter(3*n+1, s+1)
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    13
}
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    14
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    15
def collatz(n: Long) : Long = n match {
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    16
    case n if(n>0) => stepsCounter(n,0)
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    17
    case n if(n<=0) => stepsCounter(1,0)
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    18
}
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    19
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    21
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    22
//(2) Complete the collatz_max function below. It should
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    23
//    calculate how many steps are needed for each number 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    24
//    from 1 up to a bound and then calculate the maximum number of
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    25
//    steps and the corresponding number that needs that many 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    26
//    steps. Again, you should expect bounds in the range of 1
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    27
//    up to 1 Million. The first component of the pair is
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    28
//    the maximum number of steps and the second is the 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    29
//    corresponding number.
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    30
323
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    31
def collatz_max(bnd: Long) : (Long, Long) =  {
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    32
    val allCollatz = for(i<-1L until bnd) yield collatz(i)
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    33
    val pair = (allCollatz.max, (allCollatz.indexOf(allCollatz.max) +1).toLong)
1f8005b4cdf6 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    34
    pair
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
}
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    37
}