You could stay with an i
, till your if
-statement is false
.
In other words:
If a number can be divided by i
, then don't immediately go to i+1
, but try to divide by i
again.
EDIT: I think the easiest way is to use a DO WHILE
-loop. To calculate the divisor, you have to multiply all your i
.
gcd = 1
DO i=2,10,1
DO WHILE (MOD(n1,i).EQ.0.AND.MOD(n2,i).EQ.0)
n1=n1/i
n2=n2/i
gcd = gcd * i
ENDDO
ENDDO
WRITE(*,*) gcd