Problem 32

This commit is contained in:
2026-03-20 11:50:08 +01:00
parent a587ff7362
commit fdba322ec8
2 changed files with 33 additions and 0 deletions
@@ -0,0 +1,15 @@
package ninetyNineScalaProblems.arithmetic
import scala.annotation.tailrec
object Problem32:
def gcd(a: Int, b: Int): Int =
@tailrec
def loop(cursor: Tuple2[Int, Int]): Int =
cursor match
case (a, 0) =>
a
case (a, b) =>
loop((b, a % b))
loop((a, b))
@@ -0,0 +1,18 @@
package ninetyNineScalaProblems.arithmetic
class Problem32Suite extends munit.FunSuite:
List(
((0, 0), 0),
((1, 1), 1),
((2, 2), 2),
((1, 0), 1),
((0, 1), 1),
((1, 3), 1),
((3, 1), 1),
((2, 4), 2),
((4, 2), 2),
((6, 9), 3),
).foreach:
case ((a, b), expected) =>
test(s"gcd returns [$expected]"):
assertEquals(Problem32.gcd(a, b), expected)