diff -r c2d36d8ee2a7 -r 2e13dedd922e testing1/collatz.scala --- a/testing1/collatz.scala Wed Nov 07 12:08:01 2018 +0000 +++ b/testing1/collatz.scala Thu Nov 08 23:42:03 2018 +0000 @@ -1,25 +1,28 @@ // Part 1 about the 3n+1 conjecture //================================== -package CW6a -object CW6a { +//object CW6a { // for purposes of generating a jar def collatz(n: Long): Long = - if (n == 1) 1 else + if (n == 1) 0 else if (n % 2 == 0) 1 + collatz(n / 2) else 1 + collatz(3 * n + 1) def collatz_max(bnd: Long): (Long, Long) = { - val all = for (i <- (1 to bnd.toInt).toList) yield collatz(i) - val max = all.max - (max, all.indexOf(max) + 1) + val all = for (i <- (1L to bnd)) yield (collatz(i), i) + all.maxBy(_._1) } +/* some test cases +val bnds = List(10, 100, 1000, 10000, 100000, 1000000) +for (bnd <- bnds) { + val (steps, max) = collatz_max(bnd) + println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}") } - +*/ -(1 to 10).map(collatz(_)) +//}