The Greatest Common Divisor, GCD for short, of two positive integers can be computed with Euclid's division algorithm. Let the given numbers be a and b, a >= b. Euclid's division algorithm has the following steps:
! ---------------------------------------------------------
! This program computes the GCD of two positive integers
! using the Euclid method. Given a and b, a >= b, the
! Euclid method goes as follows: (1) dividing a by b yields
! a reminder c; (2) if c is zero, b is the GCD; (3) if c is
! no zero, b becomes a and c becomes c and go back to
! Step (1). This process will continue until c is zero.
! ---------------------------------------------------------
PROGRAM GreatestCommonDivisor
IMPLICIT NONE
INTEGER :: a, b, c
WRITE(*,*) 'Two positive integers please --> '
READ(*,*) a, b
IF (a < b) THEN ! since a >= b must be true, they
c = a ! are swapped if a < b
a = b
b = c
END IF
DO ! now we have a <= b
c = MOD(a, b) ! compute c, the reminder
IF (c == 0) EXIT ! if c is zero, we are done. GCD = b
a = b ! otherwise, b becomes a
b = c ! and c becomes b
END DO ! go back
WRITE(*,*) 'The GCD is ', b
END PROGRAM GreatestCommonDivisor
Click here to download this program.
Two positive integers please --> 46332 71162 The GCD is 26
Two positive integers please --> 128 32 The GCD is 32
Two positive integers please --> 100 101 The GCD is 1
Two positive integers please --> 97 97 The GCD is 97
would the following change to the WRITE statement work?The GCD of 46332 and 71162 is 26
WRITE(*,*) 'The GCD of ', a, ' and ', b, ' is ', b