Problem 34

This commit is contained in:
2026-03-20 12:20:34 +01:00
parent bdccd35b9e
commit cba4951ac3
2 changed files with 31 additions and 0 deletions
@@ -0,0 +1,17 @@
package ninetyNineScalaProblems.arithmetic
import scala.annotation.tailrec
object Problem34:
def totient(int: Int): Int =
@tailrec
def loop(cursor: Int, acc: Int): Int =
cursor match
case c if c == int =>
acc
case c if Problem33.coprime(c, int) =>
loop(cursor + 1, acc + 1)
case _ =>
loop(cursor + 1, acc)
loop(0, 0)
@@ -0,0 +1,14 @@
package ninetyNineScalaProblems.arithmetic
class Problem34Suite extends munit.FunSuite:
List(
(0, 0),
(1, 1),
(2, 1),
(3, 2),
(42, 12),
(10, 4),
).foreach:
case (int, expected) =>
test(s"totient returns [$expected]"):
assertEquals(Problem34.totient(int), expected)