mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
math: add gcd and lcm
Add gcd and lcm functions to calculate the greatest common divisor (gcd) and the least common multiple (lcm) to the math module. This will also work for BigInts that implements its own gcd/lcm.
This commit is contained in:
committed by
Christoffer Lerno
parent
061c02306f
commit
62dca4f1c5
@@ -194,4 +194,28 @@ fn void! test_muldiv()
|
||||
|
||||
ichar[<4>] k = {20, 30, 40, 50};
|
||||
assert(k.muldiv(20,-10) == ichar[<4>]{-40,-60,-80,-100});
|
||||
}
|
||||
}
|
||||
|
||||
fn void! test_gcd() @test
|
||||
{
|
||||
assert(math::gcd(20,15) == 5);
|
||||
assert(math::gcd(15,20) == 5);
|
||||
assert(math::gcd(-15,20) == 5);
|
||||
assert(math::gcd(15,-20) == 5);
|
||||
assert(math::gcd(-15,-20) == 5);
|
||||
assert(math::gcd(5,15,20) == 5);
|
||||
assert(math::gcd(1,2,3) == 1);
|
||||
assert(math::gcd(2,4,6,8) == 2);
|
||||
}
|
||||
|
||||
fn void! test_lcm() @test
|
||||
{
|
||||
assert(math::lcm(4,5) == 20);
|
||||
assert(math::lcm(6,10) == 30);
|
||||
assert(math::lcm(-8,20) == 40);
|
||||
assert(math::lcm(8,-20) == 40);
|
||||
assert(math::lcm(-8,-20) == 40);
|
||||
assert(math::lcm(11,17) == 11*17);
|
||||
assert(math::lcm(11,17,227,263) == 11*17*227*263);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user