Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Calculate the Least Common Multiple of bunnies jumps.
I created solution in:
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 |
import scala.io.Source object JumpingBunnies extends App { val lines = Source.stdin.getLines().toList val bunnies = lines(1).split(" ").map(_.toInt) val commonPoint = bunnies.foldLeft(BigInt(1))(lcm(_, _)) println(commonPoint) def gcd(x: BigInt, y: BigInt): BigInt = { if(y == 0) x else gcd(y, x % y) } def lcm(x: BigInt, y: BigInt): BigInt = { (x * y) / gcd(x, y) } } |