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