program main implicit none integer*8::M,N,c,count,tmp data count/0/ read *,M,N c = gcd(M,N) M = M/c N = N/c do while(.true.) ! print '(a,i3,a,i3,a,i3)',"c:",count, " M:",M," N:",N if(N.eq.1) exit count = count + (M/N) + 1 tmp = N N = M-(M/N)*N M = tmp end do count = count + M - 1 print '(i0)', count contains recursive function gcd(a,b) result(c) integer*8::a,b,c if(b.eq.0) then c = a else c = gcd(b,MOD(a,b)) end if end function gcd end program main