diff options
Diffstat (limited to 'Scala/sandbox')
-rw-r--r-- | Scala/sandbox/3-rationals.scala | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/Scala/sandbox/3-rationals.scala b/Scala/sandbox/3-rationals.scala index 387e091..3eec82c 100644 --- a/Scala/sandbox/3-rationals.scala +++ b/Scala/sandbox/3-rationals.scala @@ -2,8 +2,10 @@ object Rationals { class Rational(x :Int, y: Int) { - def numer = x - def denom = y + private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) + private val g = gcd(x, y) + def numer = x / g + def denom = y / g def neg = new Rational(-numer, denom) |