std.rational currently implements its own custom gcf (greatest common factor) function, probably in order to ensure it can handle both std.bigint.BigInt integers and other possible "big integer" implementations.
The existing gcf is buggy as it cannot handle gcf(0, n) or gcf(n, 0) as input, failing with a divide-by-zero error: when calculating the remainder in Euclid's Algorithm, it does not take into account the possibility that one or other of the numbers may be 0, and so ends up calculating n % 0.
The optimal solution here is probably to improve Phobos' existing gcd such that it can handle any conceivable BigInt implementation. In the process it may be worthwhile to also implement the superior binary GCD algorithm.
std.rational
currently implements its own customgcf
(greatest common factor) function, probably in order to ensure it can handle bothstd.bigint.BigInt
integers and other possible "big integer" implementations.The existing
gcf
is buggy as it cannot handlegcf(0, n)
orgcf(n, 0)
as input, failing with a divide-by-zero error: when calculating the remainder in Euclid's Algorithm, it does not take into account the possibility that one or other of the numbers may be 0, and so ends up calculatingn % 0
.The optimal solution here is probably to improve Phobos' existing
gcd
such that it can handle any conceivableBigInt
implementation. In the process it may be worthwhile to also implement the superior binary GCD algorithm.See also: