View Subcategory

Greatest Common Divisor

Find the largest positive integer that divides two given numbers without a remainder. For example, the GCD of 8 and 12 is 4.

scala
def gcd(x: Int, y: Int): Int =
if (b == 0) x
else gcd(b, x % y)
erlang
-module(gcd).
-export([gcd/2]).

gcd(A, 0) -> A;
gcd(A, B) -> gcd(B, A rem B).