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
@@ -42,4 +42,20 @@ fn void test_init_string_radix()
|
||||
assert(a.equals(bigint::from_int(0o123)));
|
||||
a.init_string_radix("123", 16)!!;
|
||||
assert(a.equals(bigint::from_int(0x123)));
|
||||
}
|
||||
}
|
||||
|
||||
fn void test_gcd()
|
||||
{
|
||||
BigInt a = bigint::from_int(15);
|
||||
BigInt b = bigint::from_int(20);
|
||||
assert(a.gcd(b).equals(bigint::from_int(5)));
|
||||
assert(math::gcd(a,b).equals(bigint::from_int(5)));
|
||||
}
|
||||
|
||||
fn void test_lcm()
|
||||
{
|
||||
BigInt a = bigint::from_int(11);
|
||||
BigInt b = bigint::from_int(17);
|
||||
assert(a.lcm(b).equals(bigint::from_int(11*17)));
|
||||
assert(math::lcm(a,b).equals(bigint::from_int(11*17)));
|
||||
}
|
||||
|
||||
@@ -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