# 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

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

rasujasin + 0 comments Scala Code :

if ( x == 0) return y if( y == 0) return x if ( x==y) return x if ( x > y) { gcd(x-y,y) } else { gcd(x,y-x) }

satyajitghana191 + 0 comments Haskell Solution : just type

`gcd' = gcd`

, done.

weozUA + 0 comments Scala solution

def gcd(x: Int, y: Int): Int = { if (x == 0) y else gcd(y % x, x) }

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

Sort 39 Discussions, By:

Please Login in order to post a comment