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:
Koni Marti
2024-12-08 19:30:46 +01:00
committed by Christoffer Lerno
parent 061c02306f
commit 62dca4f1c5
4 changed files with 110 additions and 4 deletions

View File

@@ -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"
*>