Skip to content

Implementation of Euclideans Algorithm in Kotlin. #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- Christopher Milan
- Vexatos
- Björn Heinrichs
- Olav Sundfør
31 changes: 31 additions & 0 deletions contents/euclidean_algorithm/code/kotlin/Euclidean.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import kotlin.math.absoluteValue

fun euclidSub(a: Int, b: Int): Int {
var a = a.absoluteValue
var b = b.absoluteValue

while (a != b) {
if (a > b) a -= b
else b -= a
}

return a
}

fun euclidMod(a: Int, b: Int): Int {
var a = a.absoluteValue
var b = b.absoluteValue

while (b != 0) {
val tmp = b
b = a % b
a = tmp
}

return a
}

fun main(args: Array<String>) {
println(euclidSub(128 * 12, 128 * 77))
println(euclidMod(64 * 67, 64 * 81))
}
4 changes: 4 additions & 0 deletions contents/euclidean_algorithm/euclidean_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
[import:18-31, lang="c_cpp"](code/c++/euclidean.cpp)
{% sample lang="java" %}
[import:3-16, lang="java"](code/java/EuclideanAlgo.java)
{% sample lang="kotlin" %}
[import:3-13, lang="kotlin"](code/kotlin/Euclidean.kt)
{% sample lang="js" %}
[import:15-29, lang="javascript"](code/javascript/euclidean_example.js)
{% sample lang="lisp" %}
Expand Down Expand Up @@ -86,6 +88,8 @@ Modern implementations, though, often use the modulus operator (%) like so
[import:5-15, lang="c_cpp"](code/c++/euclidean.cpp)
{% sample lang="java" %}
[import:18-26, lang="java"](code/java/EuclideanAlgo.java)
{% sample lang="kotlin" %}
[import:15-26, lang="kotlin"](code/kotlin/Euclidean.kt)
{% sample lang="js" %}
[import:1-13, lang="javascript"](code/javascript/euclidean_example.js)
{% sample lang="lisp" %}
Expand Down