Skip to content
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 DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [Abs](math/abs.jule)
- [Fact](math/fact.jule)
- [Fib](math/fib.jule)
- [Gcd](math/gcd.jule)
- [Max](math/max.jule)
- [Median](math/median.jule)
- [Min](math/min.jule)
Expand Down
18 changes: 18 additions & 0 deletions math/gcd.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn Gcd(a: int, b: int): int {
let mut x = a
let mut y = b

if x < 0 {
x = -x
}
if y < 0 {
y = -y
}

for y != 0 {
let t = x % y
x = y
y = t
}
return x
}
14 changes: 14 additions & 0 deletions math/gcd_test.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#build test

use "std/testing"

#test
fn testGcd(t: &testing::T) {
t.Assert(Gcd(54, 24) == 6, "gcd(54, 24) should be 6")
t.Assert(Gcd(48, 18) == 6, "gcd(48, 18) should be 6")
t.Assert(Gcd(17, 13) == 1, "gcd(17, 13) should be 1")
t.Assert(Gcd(0, 0) == 0, "gcd(0, 0) should be 0")
t.Assert(Gcd(0, 5) == 5, "gcd(0, 5) should be 5")
t.Assert(Gcd(-12, 18) == 6, "gcd(-12, 18) should be 6")
t.Assert(Gcd(12, -18) == 6, "gcd(12, -18) should be 6")
}
Loading