-
Notifications
You must be signed in to change notification settings - Fork 109
/
EuclidAlgorithm.kt
37 lines (32 loc) · 927 Bytes
/
EuclidAlgorithm.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package other
/**
*
* Euclid's algorithm for finding the greatest common divisor
*
*/
class EuclidAlgorithm {
fun computeByDivisionWithRemainder(number1: Int, number2: Int) : Int {
var copyNumber1 = number1
var copyNumber2 = number2
while (copyNumber1 != 0 && copyNumber2 != 0) {
if (copyNumber1 > copyNumber2) {
copyNumber1 %= copyNumber2
} else {
copyNumber2 %= copyNumber1
}
}
return copyNumber1 + copyNumber2
}
fun computeBySubtraction(number1: Int, number2: Int): Int {
var copyNumber1 = number1
var copyNumber2 = number2
while (copyNumber1 != copyNumber2) {
if (copyNumber1 > copyNumber2) {
copyNumber1 -= copyNumber2
} else {
copyNumber2 -= copyNumber1
}
}
return copyNumber1
}
}