-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy patheuclidean.scala
More file actions
24 lines (20 loc) · 669 Bytes
/
euclidean.scala
File metadata and controls
24 lines (20 loc) · 669 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
object Euclid {
def euclid_sub(a: Int, b: Int): Int =
(Math.abs(a), Math.abs(b)) match {
case (0, _) | (_, 0) => 0
case (x, y) if x < y => euclid_sub(x, y - x)
case (x, y) if x > y => euclid_sub(x - y, y)
case _ => a
}
def euclid_mod(a: Int, b: Int): Int =
(Math.abs(a), Math.abs(b)) match {
case (_, 0) => a
case (a, b) => euclid_mod(b, a % b)
}
def main(args: Array[String]): Unit = {
println("[#]\nModulus-based euclidean algorithm result:")
println(euclid_mod(64 * 67, 64 * 81))
println("[#]\nSubtraction-based euclidean algorithm result:")
println(euclid_sub(128 * 12, 128 * 77))
}
}