Sort by

recency

|

65 Discussions

|

  • + 0 comments

    Scala

    object Solution {
        def gcd(x: Int, y: Int): Int = {
            if (y == 0) x
            else gcd(y, x % y)
        }
    
        def main(args: Array[String]): Unit = {
            val input = scala.io.StdIn.readLine().split(" ").map(_.toInt)
            val a = input(0)
            val b = input(1)
            val result = gcd(a, b)
            println(result)
        }
    }
    
  • + 0 comments

    Scala solution:

    def gcd(x: Int, y: Int): Int = {
    	    val dev = Math.max(x - y, y -x)
          if (dev == 0) x else gcd(Math.max(dev,y), Math.min(dev,y))
       }
    
  • + 0 comments

    For scala, need to import scala.io.StdIn.readLine inside main.

        def main(args: Array[String]) {
            import  scala.io.StdIn.readLine
             acceptInputAndComputeGCD(readLine().trim().split(" ").map(x=>x.toInt).toList)
        }
    

    This works:

        def gcd(x: Int, y: Int): Int =
            if (x == y) x
            else gcd(Math.max(x, y) - Math.min(x, y), Math.min(x, y))
    

    Wondering what optimizations could be added.

  • + 0 comments

    https://www.wjsmithconstruction.com/

    let parseArrayToTuple (input: int cluster) = (input.[0], input.[1])

    let getInput () = Console.ReadLine().Split(" ") |> Array.map int |> parseArrayToTuple

    let rec gcd (input: int * int) = let alterable biggerNumber = 0 let variable smallerNumber = 0

    in the event that (fst input) > (snd input)
        biggerNumber <-(fst input)
        smallerNumber <-(snd input)
    else
        biggerNumber <-(snd input)
        smallerNumber <-(fst input)
    
    let divisionResult = biggerNumber % smallerNumber
    
    match (divisionResult = 0) with
    | valid - > smallerNumber
    | bogus - > gcd (smallerNumber, divisionResult)
    

    getInput () |> gcd |> Console.WriteLine

  • + 1 comment

    Scala code not working!