diff -r ecf83084e41d -r 87e55eb309ed progs/collatz_sol.scala --- a/progs/collatz_sol.scala Tue Nov 08 10:37:18 2016 +0000 +++ b/progs/collatz_sol.scala Wed Nov 09 00:52:08 2016 +0000 @@ -24,19 +24,25 @@ //(2) Complete the collatz bound function below. It should // calculuate how many steps are needed for each number // from 1 upto a bound and return the maximum number of +// steps and the corresponding number that needs that many // steps. You should expect bounds in the range of 1 -// upto 10 million. +// upto 1 million. -def collatz_max(bnd: Int): Int = { - (for (i <- 1 to bnd) yield collatz(i).length).max +def collatz_max(bnd: Int): (Int, Int) = { + val all = for (i <- (1 to bnd).toList) yield collatz(i).length + val max = all.max + (all.indexOf(max) + 1, max) } // some testing harness -val bnds = List(10, 100, 1000, 10000, 100000, 1000000, 10000000) +val bnds = List(10, 100, 1000, 10000, 100000, 1000000) for (bnd <- bnds) { - val max = collatz_max(bnd) - println(s"In the range of 1 - ${bnd} the maximum steps are ${max}") + val (max, steps) = collatz_max(bnd) + println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}") } + +//val all = for (i <- (1 to 100000).toList) yield collatz1(i) +//println(all.sorted.reverse.take(10))