If you're interested in how you'd write GCD in Dyalog, if Dyalog didn't have a primitive for it, (i.e. using other algorithms mentioned on this page: iterative, recursive, binary recursive), see different ways to write GCD in Dyalog.

GCD of 1 and 1 is 1
GCD of 1 and 10 is 1
GCD of 10 and 100 is 10
GCD of 5 and 50 is 5
GCD of 8 and 24 is 8
GCD of 36 and 1 is 1
GCD of 36 and 2 is 2
..
GCD of 36 and 16 is 4
GCD of 36 and 17 is 1
GCD of 36 and 18 is 18
..
..
GCD of 36 and 33 is 3
GCD of 36 and 34 is 2
GCD of 36 and 35 is 1

GCD of 1 and 1 is 1
GCD of 1 and 10 is 1
GCD of 10 and 100 is 10
GCD of 5 and 50 is 5
GCD of 8 and 24 is 8
GCD of 36 and 1 is 1
GCD of 36 and 2 is 2
..
GCD of 36 and 16 is 4
GCD of 36 and 17 is 1
GCD of 36 and 18 is 18
..
..
GCD of 36 and 33 is 3
GCD of 36 and 34 is 2
GCD of 36 and 35 is 1

PROGRAM EUCLIDE! calculate G.C.D. between two integer numbers! using Euclidean algorithm

!VAR J%,K%,MCD%,A%,B%

BEGIN PRINT(CHR$(12);"Input two numbers : ";) !CHR$(147) in C-64 version INPUT(J%,K%) A%=J% B%=K% WHILE A%<>B% DO IF A%>B% THEN A%=A%-B% ELSE B%=B%-A% END IF END WHILE MCD%=A% PRINT("G.C.D. between";J%;"and";K%;"is";MCD%)END PROGRAM

Note that +. is a single, two character token. GCD is a primitive in J (and anyone that has studied the right kind of mathematics should instantly recognize why the same operation is used for both GCD and OR -- among other things, GCD and boolean OR both have the same identity element: 0, and of course they produce the same numeric results on the same arguments (when we are allowed to use the usual 1 bit implementation of 0 and 1 for false and true) - more than that, though, GCD corresponds to George Boole's original "Boolean Algebra" (as it was later called). The redefinition of "Boolean algebra" to include logical negation came much later, in the 20th century).

gcd could also be defined recursively, if you do not mind a little inefficiency:

def recursive_gcd(a; b): if b == 0 then a else recursive_gcd(b; a % b) end ;

Recent versions of jq include support for tail recursion optimization for arity-0 filters (which can be thought of as arity-1 functions), so here is an implementation that takes advantage of that optimization. Notice that the subfunction, rgcd, can be easily derived from recursive_gcd above by moving the arguments to the input:

It may be helpful to read about Recursion in LabVIEW.
This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.

result is always positive, except for gcd(0,0) which is 0
atom parameters allow greater precision, but any fractional parts are immediately and deliberately discarded.
Actually, it is an autoinclude, reproduced below. The first parameter can be a sequence, in which case the second parameter (if provided) is ignored.

Racket provides a built-in gcd function. Here's a program that computes the gcd of 14 and 63:

#lang racket

(gcd 14 63)

Here's an explicit implementation. Note that since Racket is tail-calling, the memory behavior of this program is "loop-like", in the sense that this program will consume no more memory than a loop-based implementation.

The GCD subroutine can handle any number of arguments, it can also handle any number of integers within any
argument(s), making it easier to use when computing Frobenius numbers (also known as postage stamp or
coin numbers).

GCD (Greatest Common Divisor) of 0,0 is 0
GCD (Greatest Common Divisor) of 55,0 is 55
GCD (Greatest Common Divisor) of 0,66 is 66
GCD (Greatest Common Divisor) of 7,21 is 7
GCD (Greatest Common Divisor) of 41,47 is 1
GCD (Greatest Common Divisor) of 99,51 is 3
GCD (Greatest Common Divisor) of 24,-8 is 8
GCD (Greatest Common Divisor) of -36,9 is 9
GCD (Greatest Common Divisor) of -54,-6 is 6
GCD (Greatest Common Divisor) of 14,0,7 is 7
GCD (Greatest Common Divisor) of 14,7,0 is 7
GCD (Greatest Common Divisor) of 0,14,7 is 7
GCD (Greatest Common Divisor) of 15,10,20,30,55 is 5
GCD (Greatest Common Divisor) of 137438691328,2305843008139952128 is 262144

the GCD of 7 and 21 is 7 as expected
the GCD of 4 and 7 is 1 as expected
the GCD of 24 and -8 is 8 as expected
the GCD of 55 and 0 is 55 as expected
the GCD of 99 and 15 is 3 as expected
the GCD of 15 and 10 and 20 and 30 and 55 is 5 as expected
the GCD of 496 and 8128 is 16 as expected
the GCD of 496 and 8128 is 16 *** wrong. expected: 8
the GCD of 0 and 0 is 0 as expected

:Print/|?p/s/[^~]*/&\~&/s/\(.*|P\)\([^|]*\)/\\2\1/s/\([^~]*\)\n\([^~]*\)\(.*|P\)/\1\3\2/hs/~.*///./{ s/.//; p; }# Just s/.//p would work if we knew we were running under the -n option.# Using l vs p would kind of do \ continuations, but would break strings.g

:pops/[^~]*~//b next

:loads/\(.*|?.\)\(.\)/\20~\1/s/^\(.\)0\(.*|r\1\([^~|]*\)~\)/\1\3\2/s/.//b next

:Loads/\(.*|?.\)\(.\)/\2\1/s/^\(.\)\(.*|r\1\)\([^~|]*~\)/|\3\2//^|/!i\register emptys/.//b next

:trunc# for efficiency, doesn't pad with 0s, so 10k 2 5/ returns just .40# The X* here and in a couple other places works around a SunOS 4.x sed bug.s/\([^.~]*\.*\)\(.*|K\([^|]*\)\)/\3;9876543210009909:\1,\2/:trunc1 s/^\([^;]*\)\([1-9]\)\(0*\)\([^1]*\2\(.\)[^:]*X*\3\(9*\)[^,]*\),\([0-9]\)/\1\5\6\4\7,/t trunc1s/[^:]*:\([^,]*\)[^~]*/\1/b normal

:addsub1 s/\(.\{0,1\}\)\(~[^,]*\)\([0-9]\)\(\.*\),\([^;]*\)\(;\([^;]*\(\3[^;]*\)\).*X*\1\(.*\)\)/\2,\4\5\9\8\7\6/ s/,\([^~]*~\).\{10\}\(.\)[^;]\{0,9\}\([^;]\{0,1\}\)[^;]*/,\2\1\3/# could be done in one s/// if we could have >9 back-refs.../^~.*~;/!b addsub1

:exp# This decimal method is just a little faster than the binary method done# totally in dc: 1LaKLb [kdSb*LbK]Sb [[.5]*d0ktdSa<bkd*KLad1<a]Sa d1<a kk*/^[^~]*\./i\fraction in exponent ignoreds,[^-0-9].*,;9d**dd*8*d*d7dd**d*6d**d5d*d*4*d3d*2lbd**1lb*0,:exp1 s/\([0-9]\);\(.*\1\([d*]*\)[^l]*\([^*]*\)\(\**\)\)/;dd*d**d*\4\3\5\2/t exp1Gs,-*.\{9\}\([^9]*\)[^0]*0.\(.*|?.\),\2~saSaKdsaLb0kLbkK*+k1\1LaktsbkLax,s,|?.,&SadSbdXSaZla-SbKLaLadSb[0Lb-d1lb-*d+K+0kkSb[1Lb/]q]Sa0>a[dk]sadK<a[Lb],b next

println("Iterative: GCD of \(a) and \(b) is \(gcd(a, b))") println("Recursive: GCD of \(a) and \(b) is \(gcdr(a, b))")}

Output:

Iterative: GCD of 1 and 1 is 1
Recursive: GCD of 1 and 1 is 1
Iterative: GCD of 100 and -10 is 10
Recursive: GCD of 100 and -10 is 10
Iterative: GCD of 10 and -100 is 10
Recursive: GCD of 10 and -100 is 10
Iterative: GCD of -36 and -17 is 1
Recursive: GCD of -36 and -17 is 1
Iterative: GCD of 27 and 18 is 9
Recursive: GCD of 27 and 18 is 9
Iterative: GCD of 30 and -42 is 6
Recursive: GCD of 30 and -42 is 6

This doesn't need to be defined because it's a library function, but
it can be defined like this based on a recursive implementation of
Euclid's algorithm. This isn't the simplest possible solution because
it includes a bit shifting optimization that happens when both operands
are even.

A note on the last example: using brackets forces a and b to be evaluated before GCD is called. Not doing this will cause a compile error because a and b are not the same type as in the function declaration (they are Variant, not Long). Alternatively you can use the conversion function CLng as in print GCD(CLng(a),CLng(b))

WScript.Echo "The GCD of 48 and 18 is " & GCD(48,18) & "."WScript.Echo "The GCD of 1280 and 240 is " & GCD(1280,240) & "."WScript.Echo "The GCD of 1280 and 240 is " & GCD(3475689,23566319) & "."WScript.Echo "The GCD of 1280 and 240 is " & GCD(123456789,234736437) & "."

Output:

The GCD of 48 and 18 is 6.
The GCD of 1280 and 240 is 80.
The GCD of 1280 and 240 is 7.
The GCD of 1280 and 240 is 3.