--- a/testing1/collatz.scala Tue Nov 05 00:41:02 2019 +0000
+++ b/testing1/collatz.scala Wed Nov 06 00:36:45 2019 +0000
@@ -7,11 +7,12 @@
object CW6a {
-def collatz(n: Long): Long =
+/*
+ * def collatz(n: Long): Long =
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 <- (1L to bnd)) yield (collatz(i), i)
@@ -30,6 +31,20 @@
*/
+
+
+def collatz(n: Long) : Long = {
+ if (n == 1) {
+ 1L
+ } else {
+ if (n % 2 == 0) {
+ collatz(n/2) + 1
+ } else {
+ collatz((n*3)+1) + 1
+ }
+ }
+}
+
}