object HelloWorld { def main(args: Array[String]) { println(divide(-1, 2)(3, 4)) } def divide(p: (Int, Int))(q: (Int, Int)): Either[String, (Int, Int)] = { def divTrueInp (x: (Int, Int)): Boolean = if (Math.abs(x._1) < Math.abs(x._2)) false else true var out = (p._1 * q._2, p._2 * q._1 ) var in = 0 if (out._1 > out._2) in = out._1 else in = out._2 for (i <- 1 to in) { if (out._1 % i == 0 && out._2 % i == 0) { val x1 = out._1 / i val y1 = out._2 / i out = (x1, y1) } } if (p._2 == 0 || q._2 == 0 ) Left("Zero divisor") else if (divTrueInp(p) || divTrueInp(q)) Left("Invalid input") else if (divTrueInp(out) || out._2 == 0) Left("Improper result") else Right(out) } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more