# HG changeset patch # User Christian Urban # Date 1698924730 0 # Node ID 6a77c260c8a590882123615115ead4b0831371a4 # Parent 135bf034ac30ab2447848bae3b97db515f1e9629 updated diff -r 135bf034ac30 -r 6a77c260c8a5 core_testing1/collatz.scala --- a/core_testing1/collatz.scala Wed Nov 01 15:01:32 2023 +0000 +++ b/core_testing1/collatz.scala Thu Nov 02 11:32:10 2023 +0000 @@ -1,33 +1,27 @@ -import scala.annotation.tailrec // Core Part 1 about the 3n+1 conjecture //============================================ object C1 { -@tailrec -private def collatz(n: Long, steps: Long = 0): Long = { - if (n == 1) steps - else if (n % 2 == 0) collatz(n / 2, steps + 1) - else collatz(n * 3 + 1, steps + 1) -} - -def collatz_max(upper: Long): (Long, Long) = { - (1L to upper).map(n => (collatz(n), n)).maxBy(_._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) -private def is_pow_of_two(n: Long) : Boolean = { - (n & (n - 1)) == 0 +def collatz_max(bnd: Long): (Long, Long) = { + val all = for (i <- (1L to bnd)) yield (collatz(i), i) + all.maxBy(_._1) } -private def is_hard(n: Long) : Boolean = { - is_pow_of_two(3 * n + 1) -} +def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0 + +def is_hard(n: Long) : Boolean = is_pow(3 * n + 1) - -private def last_odd(n: Long): Long = { - (1L to n).filter(is_hard).max -} +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) } diff -r 135bf034ac30 -r 6a77c260c8a5 core_testing1/collatz_test.sh --- a/core_testing1/collatz_test.sh Wed Nov 01 15:01:32 2023 +0000 +++ b/core_testing1/collatz_test.sh Thu Nov 02 11:32:10 2023 +0000 @@ -15,20 +15,21 @@ # compilation tests function scala_compile { - (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> c$out 1> c$out) + (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala-cli compile "$1" ) #2> c$out 1> c$out) } # functional tests function scala_assert { - (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -nc -i "$1" -- "$2" -e "" 2> /dev/null 1> /dev/null) + (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala-cli -i "$1" "$2" -e "urbanmain()" ) #2> /dev/null 1> /dev/null) } # purity test +function scala_vars { + (sed 's/immutable/ok/g' c$out > cb$out; + egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|AtomicInteger|mutable|util.control|new Array' cb$out 2> /dev/null 1> /dev/null) +} -function scala_vars { - (egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|AtomicInteger|mutable|util.control|new Array' c$out 2> /dev/null 1> /dev/null) -} ### compilation test diff -r 135bf034ac30 -r 6a77c260c8a5 core_testing1/collatz_test1.scala --- a/core_testing1/collatz_test1.scala Wed Nov 01 15:01:32 2023 +0000 +++ b/core_testing1/collatz_test1.scala Thu Nov 02 11:32:10 2023 +0000 @@ -1,8 +1,10 @@ -import C1._ +def urbanmain() = { + import C1._ -assert(collatz(1) == 0) -assert(collatz(6) == 8) -assert(collatz(9) == 19) + assert(collatz(1) == 0) + assert(collatz(6) == 8) + assert(collatz(9) == 19) +} diff -r 135bf034ac30 -r 6a77c260c8a5 core_testing1/collatz_test2.scala --- a/core_testing1/collatz_test2.scala Wed Nov 01 15:01:32 2023 +0000 +++ b/core_testing1/collatz_test2.scala Thu Nov 02 11:32:10 2023 +0000 @@ -1,8 +1,12 @@ -import C1._ + +def urbanmain() = { + + import C1._ -assert(collatz_max(10) == (19, 9)) -assert(collatz_max(100) == (118, 97)) -assert(collatz_max(1000) == (178, 871)) -assert(collatz_max(10000) == (261, 6171)) -assert(collatz_max(100000) == (350, 77031)) -assert(collatz_max(1000000) == (524, 837799)) + assert(collatz_max(10) == (19, 9)) + assert(collatz_max(100) == (118, 97)) + assert(collatz_max(1000) == (178, 871)) + assert(collatz_max(10000) == (261, 6171)) + assert(collatz_max(100000) == (350, 77031)) + assert(collatz_max(1000000) == (524, 837799)) +} diff -r 135bf034ac30 -r 6a77c260c8a5 core_testing1/collatz_test3.scala --- a/core_testing1/collatz_test3.scala Wed Nov 01 15:01:32 2023 +0000 +++ b/core_testing1/collatz_test3.scala Thu Nov 02 11:32:10 2023 +0000 @@ -1,4 +1,8 @@ -assert(C1.last_odd(113) == 85) -assert(C1.last_odd(84) == 21) -assert(C1.last_odd(605) == 341) +def urbanmain() = { + + assert(C1.last_odd(113) == 85) + assert(C1.last_odd(84) == 21) + assert(C1.last_odd(605) == 341) + +}