Hackerrank – Computing the GCD
Problem Statement A description of the problem can be found on Hackerrank. Solution I used Euclid’s algorithm for computing Greatest Common Divisor. I created solution in: Scala All solutions are also available on my GitHub. Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
object ComputingGcd { def gcd(x: Int, y: Int): Int = { if(y == 0) x else gcd(y, x % y) } /**This part handles the input/output. Do not change or modify it **/ def acceptInputAndComputeGCD(pair:List[Int]) = { println(gcd(pair.head,pair.reverse.head)) } def main(args: Array[String]) { /** The part relates to the input/output. Do not change or modify it **/ acceptInputAndComputeGCD(readLine().trim().split(" ").map(x=>x.toInt).toList) } } |