Problem 34
This commit is contained in:
@@ -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)
|
||||||
Reference in New Issue
Block a user