From 5017f8d872159dacdf228506cf7f66462d777d48 Mon Sep 17 00:00:00 2001 From: Rui Lima Date: Sat, 21 Dec 2019 13:59:50 +0000 Subject: [PATCH] Replace gcd implementation with dart:core's --- lib/rational.dart | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/lib/rational.dart b/lib/rational.dart index 5202e14..551efa7 100644 --- a/lib/rational.dart +++ b/lib/rational.dart @@ -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 { factory Rational(BigInt numerator, [BigInt denominator]) { denominator ??= _i1; @@ -50,7 +39,7 @@ class Rational implements Comparable { } 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); @@ -96,7 +85,7 @@ class Rational implements Comparable { : 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;