# Greatest Common Divisor

Your task is to compute the greatest common divisor (GCD) of two given integers in as few bytes of code as possible.

You may write a program or function, taking input and returning output via any of our accepted standard methods (including STDIN/STDOUT, function parameters/return values, command-line arguments, etc.).

Input will be two non-negative integers. You should be able to handle either the full range supported by your language's default integer type, or the range [0,255], whichever is greater. You are guaranteed that at least one of the inputs will be non-zero.

You are not allowed to use built-ins that compute either the GCD or the LCM (least common multiple).

Standard rules apply.

### Test Cases

0 2     => 2
6 0     => 6
30 42   => 6
15 14   => 1
7 7     => 7
69 25   => 1
21 12   => 3
169 123 => 1
20 142  => 2
101 202 => 101

• If we're allowing asm to have inputs in whatever registers are convenient, and the result in whatever reg is convenient, we should definitely be allowing functions, or even code fragments (i.e. just a function body). Making my answer a complete function would add about 4B with a register calling convention like MS's 32bit vectorcall (one xchg eax, one mov, and a ret), or more with a stack calling convention. – Peter Cordes Apr 8 '16 at 23:05
• @PeterCordes Sorry, I should have been more specific. You can totally just write the bear necessary code but if you would be so kind as to include a way to run said code it would be nice. – Mike Shlanta Apr 9 '16 at 18:59
• So count just the gcd code, but provide the surrounding code so people can verify / experiment / improve? BTW, your test-cases with zero as one of the two inputs break our x86 machine code answers. div by zero raises a hardware exception. On Linux, your process gets a SIGFPE. – Peter Cordes Apr 9 '16 at 19:30
• @CodesInChaos Memory and time limitations are usually ignored as long as the algorithm itself can in principle handle all inputs. The rule is just meant to avoid people hardcoding arbitrary limits for loops that artificially limits the algorithm to a smaller range of inputs. I don't quite see how immutability comes into this? – Martin Ender Apr 10 '16 at 14:10
• gcd(0,n) is error not n – user58988 Mar 18 '19 at 12:33

# Forth (gforth), 37 34 bytes

: f dup if tuck mod recurse then ;


Try it online!

Same as the code below, but using recursion instead. Turns out that the combination if then recurse is shorter than begin while repeat by 3 chars, even though the word recurse looks quite bulky.

### Previous solution, 37 bytes

: f begin dup while tuck mod repeat ;


Try it online!

Input is two single-cell integers, output is one double-cell integer.

### How it works

A "cell" means a space for one item on the stack. A double-cell integer in Forth takes two cells, where the most significant cell is on the top. For this challenge, the GCD of two single-cell integers always fits in a cell, so the upper cell is always 0.

: f ( a b -- d ) \ Define a function f which takes two singles and gives a double
begin          \ Start a loop
dup while    \   While b is not zero...
tuck         \   Copy b under a ( stack: b a b ) and
mod          \   Calculate a%b  ( stack: b a%b )
\   That is, change ( a b ) to ( b a%b )
repeat ;       \ End loop
\ The result is ( gcd 0 ) which is gcd in double-cell


# Pyth, 2 bytes

iE


Try it here!

Input as integer on two separate lines. Just uses a builtin.

• Haha, I like it. – Mike Shlanta Apr 7 '16 at 18:06
• If you change the input format, iF also works. In addition, I think M?HgH%GHG is the shortest no-builtin way of doing this. – FryAmTheEggman Apr 7 '16 at 19:39
• I think the question disallows using a GCD builtin. – Cyoce Apr 9 '16 at 23:10

# Befunge-93, 21 bytes

&&:v_.@
00:_^#:%g00\p


Try it Online

Yet another Euclidean algorithm

# Japt, 9 bytes

V?ßVU%V:U


Run it online

8 bytes if we can reverse the order of the input:

?ßV%UU:V


Run it online

# Ink, 29 bytes

=i(a,b)
{b:->i(b,a%b)}{a}->->


Try it online!

# AWK, 39 bytes

{for(x=$1>$2?$1:$2;$1%x||$2%x;)--x}$0=x  Try it online! Does require that 1 of the inputs be positive. Nothing fancy, but I don't see another AWK solution. ## Perl 6, 28 bytes my&f={$^b??f($b,$^a%$b)!!$a}


# Forth (gforth), 52 bytes

: f begin 2dup max -rot min tuck mod ?dup 0= until ;


Try it online!

Uses Euclidean Algorithm [repeatedly call larger % smaller until result is 0]

### Code Explanation

: f               \ start a new word definition
begin           \ start and indefinite loop
2dup          \ duplicate arguments
max -rot min  \ reorder arguments so the smaller is on top
tuck          \ make a copy of the smaller argument and move it behind the larger
mod ?dup 0=   \ get the modulo of the two arguments, then duplicate and check if it is 0
until           \ end the loop if it is
;                 \ end the word definition



# Ral, 45 37 bytes

,:,+0=0*/-:1+1:+1+:+:1=?0*+0*/:0=1*?.


Try it online! (inputs on separate lines)

Commented:

,:,                  Input a and b