Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace gcd implementation with dart:core's #23

Merged
merged 1 commit into from
Dec 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions lib/rational.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ final _i5 = BigInt.from(5);
final _i10 = BigInt.from(10);
final _i31 = BigInt.from(31);

BigInt _gcd(BigInt a, BigInt b) {
var q = a;
var r = b;
while (r != _i0) {
final d = r;
r = q % d;
q = d;
}
return q;
}

class Rational implements Comparable<Rational> {
factory Rational(BigInt numerator, [BigInt denominator]) {
denominator ??= _i1;
Expand All @@ -50,7 +39,7 @@ class Rational implements Comparable<Rational> {
}
final aNumerator = numerator.abs();
final aDenominator = denominator.abs();
final gcd = _gcd(aNumerator, aDenominator);
final gcd = aNumerator.gcd(aDenominator);
return (gcd == _i1)
? Rational._normalized(numerator, denominator)
: Rational._normalized(numerator ~/ gcd, denominator ~/ gcd);
Expand Down Expand Up @@ -96,7 +85,7 @@ class Rational implements Comparable<Rational> {
: assert(numerator != null),
assert(denominator != null),
assert(denominator > _i0),
assert(_gcd(numerator.abs(), denominator) == _i1);
assert(numerator.abs().gcd(denominator) == _i1);

final BigInt numerator, denominator;

Expand Down