summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-04-09 22:41:26 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-10 18:03:22 +0100
commit6bffe17cb037b5c102cfca1dfb75376625784743 (patch)
tree9534fecafd1e51ddc3dfdfb3b54f6211c57f790c
parent857838ed6133691aa99c097f522acd4484b953f9 (diff)
downloadcoursera-6bffe17cb037b5c102cfca1dfb75376625784743.zip
coursera-6bffe17cb037b5c102cfca1dfb75376625784743.tar.gz
Scala : add private gcd in rational constructor
-rw-r--r--Scala/sandbox/3-rationals.scala6
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)