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
@@ -328,7 +328,7 @@ fn BigInt BigInt.unary_minus(&self)
|
||||
}
|
||||
|
||||
|
||||
macro void BigInt.div(self, BigInt other)
|
||||
macro BigInt BigInt.div(self, BigInt other)
|
||||
{
|
||||
self.div_this(other);
|
||||
return self;
|
||||
@@ -831,6 +831,14 @@ fn BigInt BigInt.gcd(&self, BigInt other)
|
||||
return g;
|
||||
}
|
||||
|
||||
fn BigInt BigInt.lcm(&self, BigInt other)
|
||||
{
|
||||
BigInt x = self.abs();
|
||||
BigInt y = other.abs();
|
||||
BigInt g = y.mult(x);
|
||||
return g.div(x.gcd(y));
|
||||
}
|
||||
|
||||
<*
|
||||
@require bits >> 5 < MAX_LEN "Required bits > maxlength"
|
||||
*>
|
||||
|
||||
Reference in New Issue
Block a user