# Computing the GCD

# Computing the GCD

nano + 0 comments Here is a templete I wrote to handle input/output in OCaml because I had difficulty without any templete in OCaml, while a templete for Scala is provided.

I hope this might be of some help for OCaml users. If you find any mistakes or anything to rewrite, please let me know.

let rec intlst_of_strlst lst = match lst with first::rest -> int_of_string(first)::intlst_of_strlst(rest) | [] -> [] let () = let str = read_line() in let lst = Str.split (Str.regexp "[ \t]+") str in let d = intlst_of_strlst lst in let xy = Array.of_list d in let ans = gcd xy.(0) xy.(1) in print_int ans;;

mizgajski_jan + 1 comment It would be amazing if you guys could add a post submission leaderboard of solutions curated by the community. Check out how codewars do it. This feature has much more use for pro programmers than the challenges themselves - you are able to learn new constructs and approaches by example on something you have solved yourselve and understand the premises of.

For example, when solving the fibonacii challenge (very basic), I would love to see if someone used the

`@tailrec`

annotation in Scala, or how they implemented the accumulator technique and downloading every solution separatly is a real drag. The value of community curated leaderboard of solutions increseas exponentialy as the complexity of the challenge increases.abhiranjan + 1 comment Yes. Actually similar feature is in pipeline where users will moderate the ordering of submissions at leaderboard.

aminoacid + 0 comments Still waiting for it to be implemented..

shailrshah + 0 comments Here's my one-line solution:

int getGCD(int a, int b) { return (a == 0 || b == 0) ? (a + b) : (getGCD(b, a % b)); }

mrussotto + 0 comments Haskell wiseacre answer:

gcd' = gcd

soltankara + 0 comments hi, here is my solution

defmodule M do def gcd(num1, num2) do num3 = rem(num2, num1) if num3 == 0 do IO.puts num1 else gcd(num3, num1) end end def main() do {min, max} = IO.read(:stdio, :line) |> String.split |> Enum.map(&String.to_integer(&1)) |> Enum.min_max gcd(min, max) end end M.main

fluffydogcatrat + 0 comments Haskell

gcd' :: Integral a => a -> a -> a gcd' n m | n > m = gcd' (n-m) m | n < m = gcd' n (m-n) | otherwise = n

sampsonjemison + 0 comments There is no need for this to be hard:

gcd' n m = if n == m then n else gcd (mod m n) n

sayalipat + 0 comments WIsh I had known this. I did something like below which doesnt satisfy all cases :( #unneccessarilycomplicated

`public static int generalizedGCD(int num, int[] arr) { // WRITE YOUR CODE HERE // check edge values for num if(num == 1) { return arr[0]; } // iterate over arr HashMap<Integer, Integer> divisors = new HashMap<Integer, Integer>(); for(int index = 0; index < num; index++) { int start = 2; while (start <= arr[index] / 2) { if (arr[index] % start == 0) { if (divisors.containsKey(start)) { int count = divisors.get(start); count++; divisors.put(start, count); } else divisors.put(start, 1); } start++; } } Iterator<Map.Entry<Integer,Integer>> itr = divisors.entrySet().iterator(); int maxCount = -1; int gcd = -1; while(itr.hasNext()) { Map.Entry<Integer, Integer> entry = itr.next(); if(maxCount < entry.getValue()) { maxCount = entry.getValue(); gcd = entry.getKey(); } } return maxCount==num-1 ? gcd : 1; // METHOD SIGNATURE ENDS }`

agasperi + 0 comments My haskell solution:

gcd' :: Integral a => a -> a -> a gcd' n m | n == m = n gcd' n m = let r = mod x y in if r == 0 then y else gcd' r y where x = max n m y = min n m

mertalptaytak + 0 comments Scala solution without % :

def gcd(x: Int, y: Int): Int = { (x, y) match { case a if x == y => x case b if x > y => gcd(x - y, y) case c if x < y => gcd(x, y - x) } }

Sort 43 Discussions, By:

Please Login in order to post a comment