Skip to content

Commit c53c762

Browse files
olavenberquist
authored andcommitted
Implementation of Euclideans Algorithm in Kotlin. (#589)
1 parent eb17bc9 commit c53c762

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
4242
- Christopher Milan
4343
- Vexatos
4444
- Björn Heinrichs
45+
- Olav Sundfør
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import kotlin.math.absoluteValue
2+
3+
fun euclidSub(a: Int, b: Int): Int {
4+
var a = a.absoluteValue
5+
var b = b.absoluteValue
6+
7+
while (a != b) {
8+
if (a > b) a -= b
9+
else b -= a
10+
}
11+
12+
return a
13+
}
14+
15+
fun euclidMod(a: Int, b: Int): Int {
16+
var a = a.absoluteValue
17+
var b = b.absoluteValue
18+
19+
while (b != 0) {
20+
val tmp = b
21+
b = a % b
22+
a = tmp
23+
}
24+
25+
return a
26+
}
27+
28+
fun main(args: Array<String>) {
29+
println(euclidSub(128 * 12, 128 * 77))
30+
println(euclidMod(64 * 67, 64 * 81))
31+
}

contents/euclidean_algorithm/euclidean_algorithm.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
1515
[import:18-31, lang="c_cpp"](code/c++/euclidean.cpp)
1616
{% sample lang="java" %}
1717
[import:3-16, lang="java"](code/java/EuclideanAlgo.java)
18+
{% sample lang="kotlin" %}
19+
[import:3-13, lang="kotlin"](code/kotlin/Euclidean.kt)
1820
{% sample lang="js" %}
1921
[import:15-29, lang="javascript"](code/javascript/euclidean_example.js)
2022
{% sample lang="lisp" %}
@@ -88,6 +90,8 @@ Modern implementations, though, often use the modulus operator (%) like so
8890
[import:5-15, lang="c_cpp"](code/c++/euclidean.cpp)
8991
{% sample lang="java" %}
9092
[import:18-26, lang="java"](code/java/EuclideanAlgo.java)
93+
{% sample lang="kotlin" %}
94+
[import:15-26, lang="kotlin"](code/kotlin/Euclidean.kt)
9195
{% sample lang="js" %}
9296
[import:1-13, lang="javascript"](code/javascript/euclidean_example.js)
9397
{% sample lang="lisp" %}

0 commit comments

Comments
 (0)