changeset 123 | 556cd74cbba9 |
parent 64 | d6f97b562424 |
child 127 | b4def82f3f9f |
122:90dd9c6162b3 | 123:556cd74cbba9 |
---|---|
1 // Part 1 about the 3n+1 conceture |
1 // Part 1 about the 3n+1 conceture |
2 //================================= |
2 //================================= |
3 |
|
4 |
|
3 |
5 |
4 //(1) Complete the collatz function below. It should |
6 //(1) Complete the collatz function below. It should |
5 // recursively calculate the number of steps needed |
7 // recursively calculate the number of steps needed |
6 // until the collatz series reaches the number 1. |
8 // until the collatz series reaches the number 1. |
7 // If needed you can use an auxilary function that |
9 // If needed you can use an auxilary function that |
34 (all.indexOf(max) + 1, max) |
36 (all.indexOf(max) + 1, max) |
35 } |
37 } |
36 |
38 |
37 |
39 |
38 // some testing harness |
40 // some testing harness |
39 val bnds = List(2, 10, 100, 1000, 10000, 100000, 77000, 90000, 1000000) |
41 val bnds = List(2, 10, 100, 1000, 10000, 100000, 77000, 90000, 1000000, 5000000) |
40 |
42 |
41 for (bnd <- bnds) { |
43 for (bnd <- bnds) { |
42 val (max, steps) = collatz_max(bnd) |
44 val (max, steps) = collatz_max(bnd) |
43 println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}") |
45 println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}") |
44 } |
46 } |
45 |
47 |
46 |
48 |
47 //val all = for (i <- (1 to 100000).toList) yield collatz1(i) |
49 //val all = for (i <- (1 to 100000).toList) yield collatz1(i) |
48 //println(all.sorted.reverse.take(10)) |
50 //println(all.sorted.reverse.take(10)) |
51 |
|
52 |
|
53 |