42
\$\begingroup\$

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
\$\endgroup\$
6
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Peter Cordes Apr 8 '16 at 23:05
  • \$\begingroup\$ @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. \$\endgroup\$ – Mike Shlanta Apr 9 '16 at 18:59
  • \$\begingroup\$ 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. \$\endgroup\$ – Peter Cordes Apr 9 '16 at 19:30
  • 3
    \$\begingroup\$ @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? \$\endgroup\$ – Martin Ender Apr 10 '16 at 14:10
  • 2
    \$\begingroup\$ gcd(0,n) is error not n \$\endgroup\$ – user58988 Mar 18 '19 at 12:33

69 Answers 69

1 2
3
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Pyth, 2 bytes

iE

Try it here!

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

\$\endgroup\$
3
  • \$\begingroup\$ Haha, I like it. \$\endgroup\$ – Mike Shlanta Apr 7 '16 at 18:06
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – FryAmTheEggman Apr 7 '16 at 19:39
  • 7
    \$\begingroup\$ I think the question disallows using a GCD builtin. \$\endgroup\$ – Cyoce Apr 9 '16 at 23:10
0
\$\begingroup\$

Befunge-93, 21 bytes

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

Try it Online

Yet another Euclidean algorithm

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

Ink, 29 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 28 bytes

my&f={$^b??f($b,$^a%$b)!!$a}
\$\endgroup\$
0
0
\$\begingroup\$

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
    
\$\endgroup\$
0
\$\begingroup\$

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
+0=                  Add a to b
                   Loop:
0*/-                 Subtract b from a
:1+1:+1+:+:1=?       Continue if a >= 0
0*+                  Add b to a
0*/:0=               Swap a and b
1*?                  Continue if b > 0
.                    Print a
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.