Logo

Programming-Idioms

History of Idiom 74 > diff from v36 to v37

Edit summary for version 37 by jupiter:
New Fortran implementation by user [jupiter]

Version 36

2019-10-28, 21:09:45

Version 37

2019-11-15, 19:52:51

Idiom #74 Compute GCD

Compute the greatest common divisor x of big integers a and b. Use an integer type able to handle huge numbers.

Idiom #74 Compute GCD

Compute the greatest common divisor x of big integers a and b. Use an integer type able to handle huge numbers.

Imports
use,intrinsic : iso_fortran_env, only : int64
Code
function gcd(m,n) result(answer)
implicit none
integer(kind=int64),intent(in)  :: m, n
integer(kind=int64)             :: answer,irest,ifirst
   ifirst=iabs(m)
   answer=iabs(n)
   if(answer.eq.0)then
      answer=ifirst
   else
      do
         irest = mod(ifirst,answer)
         if(irest == 0)  exit
         ifirst = answer
         answer = irest
      enddo
      answer= iabs(answer)
   endif
end function gcd