• + 0 comments

    this is well know math problem of finding Least Common Multiplication. So in scala:

    import scala.io.StdIn
    
    object Solution {
      private def gcd(a: BigInt, b: BigInt): BigInt = {
        if (b == 0) a else gcd(b, a % b)
      }
    
      private def lcm(a: BigInt, b: BigInt): BigInt = {
        (a * b).abs / gcd(a, b)
      }
    
      private def meet(bunnies: Seq[Int]): BigInt = {
        if (bunnies.isEmpty)
          return 0
        bunnies.map(BigInt(_)).reduce(lcm)
      }
    
      def main(args: Array[String]): Unit = {
        StdIn.readLine()
        val bunnies = StdIn
          .readLine()
          .trim()
          .split(' ')
          .map(_.toInt)
          .toSeq
        println(meet(bunnies))
      }
    }