# Computing the GCD

# Computing the GCD

- M
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..

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;;

- MR
mrussotto + 0 comments Haskell wiseacre answer:

gcd' = gcd

- K
kryptos + 0 comments Elixir

defmodule Solution do def gcd(a,0), do: IO.puts(a) def gcd(a,b) when a > 0 do gcd(b, rem(a,b)) end end input = String.split(IO.gets("")," ") [a,b] = input |> Enum.map(fn(x) -> elem(Integer.parse(x),0) end) Solution.gcd(a,b)

kamanashisroy + 0 comments Haskell accumulator

gcd' :: Integral a => a -> a -> a gcd' 0 m = m gcd' m 0 = m gcd' n m = if m > n then gcd (n-m) m else gcd (m-n) n

dhirajhimani + 0 comments def gcd(x: Int, y: Int): Int = { if (x == y) { x } else { if (x % y == 0) { y } else gcd(y, x % y) } } def acceptInputAndComputeGCD(pair: List[Int]) = { val x = pair.head val y = pair.reverse.head var x_y = x > y println(gcd(if (x_y) x else y , if (x_y) y else x)) }

- AG
andreigasp1 + 0 comments Clojure solution

(require '[clojure.string :as str]) (defn cmmdc [x y] (if (= x y) x (if (> x y) (recur (- x y) y) (recur x (- y x))))) (let [line (map read-string (str/split (read-line) #" "))] (println (cmmdc (first line) (first (rest line)))))

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)); }

ant_hill + 0 comments Haskell solution

gcd' n m | n == m = n | m > n = gcd' n (m - n) | m < n = gcd' (n-m) m

- HK
har33sh + 0 comments gcd' n m | n == 1 = m | otherwise = gcd' ( min ( m

`mod`

n ) (n`mod`

m) ) ( max ( m`mod`

n ) (n`mod`

m) )Haskell experts, Can I know why its failing ?

Sort 29 Discussions, By:

Please Login in order to post a comment