--- a/core_testing1/collatz.scala Thu Nov 03 11:30:09 2022 +0000
+++ b/core_testing1/collatz.scala Tue Nov 08 00:27:47 2022 +0000
@@ -1,51 +1,53 @@
// Core Part 1 about the 3n+1 conjecture
-//==================================
+//============================================
+
+object C1 {
+
+// ADD YOUR CODE BELOW
+//======================
-// generate jar with
-// > scala -d collatz.jar collatz.scala
+// test1 7 Nov
+// test2
+// test3
+// test4
-object C1 { // for purposes of generating a jar
-def collatz(n: Long): Long =
+//(1)
+def collatz(n: Long) : Long =
if (n == 1) 0 else
if (n % 2 == 0) 1 + collatz(n / 2) else
1 + collatz(3 * n + 1)
+//(2)
+//def collatz_max(bnd: Long) : (Long, Long) = {
+// val all = for (i <- (1L to bnd)) yield (collatz(i), i)
+// all.maxBy(_._1)
+//}
+
def collatz_max(bnd: Long): (Long, Long) = {
val all = for (i <- (1L to bnd)) yield (collatz(i), i)
all.maxBy(_._1)
}
-//collatz_max(1000000)
-
-
-/* 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}")
-}
-
-*/
-def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0
+//(3)
+
+def is_pow_of_two(n: Long) : Boolean = (n & (n - 1)) == 0
-def is_hard(n: Long) : Boolean = is_pow(3 * n + 1)
+def is_hard(n: Long) : Boolean = is_pow_of_two(3 * n + 1)
-def last_odd(n: Long) : Long =
- if (is_hard(n)) n else
+def last_odd(n: Long) : Long = if (is_hard(n)) n else
if (n % 2 == 0) last_odd(n / 2) else
last_odd(3 * n + 1)
-
-
-//for (i <- 130 to 10000) println(s"$i: ${last_odd(i)}")
-//for (i <- 1 to 100) println(s"$i: ${collatz(i)}")
-
}
+// This template code is subject to copyright
+// by King's College London, 2022. Do not
+// make the template code public in any shape
+// or form, and do not exchange it with other
+// students under any circumstance.