diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-09 22:41:26 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:22 +0100 |
commit | 6bffe17cb037b5c102cfca1dfb75376625784743 (patch) | |
tree | 9534fecafd1e51ddc3dfdfb3b54f6211c57f790c /Scala | |
parent | 857838ed6133691aa99c097f522acd4484b953f9 (diff) | |
download | coursera-6bffe17cb037b5c102cfca1dfb75376625784743.zip coursera-6bffe17cb037b5c102cfca1dfb75376625784743.tar.gz |
Scala : add private gcd in rational constructor
Diffstat (limited to 'Scala')
-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) |