# Computing the GCD

# Computing the GCD

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

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

ddenisovdanila + 0 comments Erlang solution:

main() -> [Q,W] = string:split(io:get_line(""), " "), {A, _} = string:to_integer(Q), {B, _} = string:to_integer(W), GCD = fun F(X,Y) -> case X-Y =:= 0 of false -> ((X - Y > Y) andalso F(X-Y,Y)) orelse F(Y, X-Y); true -> io:fwrite("~p",[Y]), true end end, ((A > B) andalso GCD(A,B)) orelse GCD(B,A).

rauan_assis + 0 comments Elixir solution:

defmodule Gdc do def main do IO.read(:stdio, :line) |> String.split |> Enum.map(&String.to_integer(&1)) |> compute_gdc |> IO.inspect end def compute_gdc(list) do {min, max} = Enum.min_max(list) reminder = rem(max, min) if reminder > 0, do: compute_gdc([reminder, min]), else: min end end Gdc.main

VincentiusNicky + 0 comments easy if you watch the video and read the questions clearly :

`if(x == 0) return y; gcd(y%x,(y-(y%x))/(y/x));`

but dont forget in your main paste this code to avoid time out:

`if(pair.head > pair.reverse.head) println(gcd(pair.reverse.head ,pair.head)) else println(gcd(pair.head,pair.reverse.head))`

abratashov + 0 comments Elixir solution of 'Computing the GCD'

defmodule Solution do def gdc(x,y) do {min, max} = Enum.min_max([x,y]) reminder = rem(max, min) if reminder == 0, do: min, else: gdc(min, reminder) end def main do [x,y] = IO.gets('') |> String.split |> Enum.map(&String.to_integer(&1)) IO.puts gdc(x,y) end end Solution.main

ankitvij23 + 0 comments 5 lines of code in SCALA

def gcd(x: Int, y: Int): Int ={ val remainder = x%y remainder match { case 0 => y case _ => gcd(y, remainder) }

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)

Sort 34 Discussions, By:

Please Login in order to post a comment