1
2! Copyright (C) 2002-2005 J. K. Dewhurst, S. Sharma and C. Ambrosch-Draxl.
3! This file is distributed under the terms of the GNU Lesser General Public
4! License. See the file COPYING for license details.
5
6!BOP
7! !ROUTINE: gcd
8! !INTERFACE:
9integer function gcd(x,y)
10! !INPUT/OUTPUT PARAMETERS:
11!   x : first integer (in,integer)
12!   y : second integer (in,integer)
13! !DESCRIPTION:
14!   Computes the greatest common divisor (GCD) of two integers using Euclid's
15!   algorithm.
16!
17! !REVISION HISTORY:
18!   Created September 2004 (JKD)
19!EOP
20!BOC
21implicit none
22! arguments
23integer, intent(in) :: x,y
24! local variables
25integer a,b,c
26if ((x.le.0).or.(y.le.0)) then
27  write(*,*)
28  write(*,'("Error(gcd): x <= 0 or y <= 0 : ",2I8)') x,y
29  write(*,*)
30  stop
31end if
32if (x.ge.y) then
33  a=x
34  b=y
35else
36  a=y
37  b=x
38end if
3910 continue
40c=mod(a,b)
41a=b
42b=c
43if (c.gt.0) goto 10
44gcd=a
45end function
46!EOC
47
48