diff --git a/src/main/scala/ninetyNineScalaProblems/arithmetic/Problem35.scala b/src/main/scala/ninetyNineScalaProblems/arithmetic/Problem35.scala new file mode 100644 index 0000000..f0e752e --- /dev/null +++ b/src/main/scala/ninetyNineScalaProblems/arithmetic/Problem35.scala @@ -0,0 +1,56 @@ +package ninetyNineScalaProblems.arithmetic + +import scala.annotation.tailrec +import scala.math.sqrt + +object Problem35: + def primeNumbersUntil(start: Int, stop: Int): List[Int] = + @tailrec + def loop(cursor: Int, acc: List[Int]): List[Int] = + cursor match + case c if c > stop => + acc + case c if Problem31.isPrime(c) => + loop(cursor + 1, c :: acc) + case _ => + loop(cursor + 1, acc) + + loop(start, List()).reverse + + def smallestPrimeDivisor(n: Int): Int = + @tailrec + def loop(cursor: List[Int]): Int = + cursor match + case head :: tail if n % head == 0 => + head + case head :: tail => + loop(tail) + case Nil => + 1 + + loop(primeNumbersUntil(2, n)) + + + def primeFactors(int: Int): Option[List[Int]] = + @tailrec + def loop(cursor: Int, acc: List[Int]): List[Int] = + // Thread.sleep(500) + println(s"\n[DEBUG] cursor = $cursor") + println(s"[DEBUG] acc = $acc") + println(s"[DEBUG] smallestPrimeDivisor(cursor) = ${smallestPrimeDivisor(cursor)}") + cursor match + case c if Problem31.isPrime(c) => + println(s"[DEBUG] case1") + c :: acc + case c => + println(s"[DEBUG] case2") + val divisor = smallestPrimeDivisor(c) + loop(cursor / divisor, divisor :: acc) + + int match + case 0 => + None + case 1 => + None + case _ => + Some(loop(int, List()).sorted) \ No newline at end of file diff --git a/src/test/scala/ninetyNineScalaProblems/arithmetic/Problem35Suite.scala b/src/test/scala/ninetyNineScalaProblems/arithmetic/Problem35Suite.scala new file mode 100644 index 0000000..f18c3e6 --- /dev/null +++ b/src/test/scala/ninetyNineScalaProblems/arithmetic/Problem35Suite.scala @@ -0,0 +1,56 @@ +package ninetyNineScalaProblems.arithmetic + +class Problem35Suite extends munit.FunSuite: + List( + ((0, 0), List()), + ((1, 0), List()), + ((1, 1), List()), + ((1, 1), List()), + ((1, 2), List(2)), + ((5, 4), List()), + ((5, 1), List()), + ((1, 5), List(2, 3, 5)), + ((1, 6), List(2, 3, 5)), + ((6, 6), List()), + ((6, 7), List(7)), + ((7, 7), List(7)), + ).foreach: + case ((start, stop), expected) => + test(s"primeNumbersUntil returns [$expected]"): + assertEquals(Problem35.primeNumbersUntil(start, stop), expected) + + List( + (2, 2), + (3, 3), + (4, 2), + (5, 5), + (6, 2), + (7, 7), + (8, 2), + (9, 3), + (10, 2), + (11, 11), + (12, 2), + (13, 13), + (14, 2), + (315, 3), + (437, 19), + ).foreach: + case (n, expected) => + test(s"smallestPrimeDivisor returns [$expected]"): + assertEquals(Problem35.smallestPrimeDivisor(n), expected) + + List( + (0, None), + (1, None), + (2, Some(List(2))), + (3, Some(List(3))), + (4, Some(List(2, 2))), + (5, Some(List(5))), + (42, Some(List(2, 3, 7))), + (60, Some(List(2, 2, 3, 5))), + (315, Some(List(3, 3, 5, 7))), + ).foreach: + case (int, expected) => + test(s"primeFactors returns [$expected]"): + assertEquals(Problem35.primeFactors(int), expected) \ No newline at end of file