Sort by

recency

|

17 Discussions

|

  • + 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))
      }
    }
    
  • + 0 comments

    Haskell has an lcm function, so it's an easy solution

    main = do
        _ <- getLine
        nums <- map read . words <$> getLine
        print $ foldl lcm 1 nums 
    
  • + 0 comments

    what is the way to submit this problem in cpp..and what's the solution of it.

  • + 0 comments

    Please, why will answer to case 10 be multiple of 10; when none of the numbers in the array is multiple of 5! Meanwhile, I am only missing case 10.

  • + 0 comments

    A minimal complete solution in Haskell is rather trivial

    main = getLine >> getLine >>= print. foldr1 lcm. map read. words