if statement - Continue the search inside a DO loop -
i wrote simple fortran code in order following: assume have integer numbers n1 , n2 have common divisors. example 3 , 6 both divided 3. here code
program test integer i,n1,n2 write(*,*)' please enter 2 numbers: ' read(*,*)n1,n2 i=2,10,1 if(mod(n1,i).eq.0.and.mod(n2,i).eq.0)then n1=n1/i n2=n2/i endif n1=n1 n2=n2 enddo write(*,*)n1,n2 pause end
this works fine example (3,6). however, there cases (4,8) in numbers have more 1 common divisor, in case 2 , 4. example (16,24). want compute maximum common divisor of 2 numbers , reduce them (i.e. 3,6 1 , 2), code returns first 1 (4,8 returns 2, 4 instead of 1,2). how should modified in order calculate maximum divisor?
many in advance!
you stay i
, till if
-statement false
.
in other words:
if number can divided i
, don't go i+1
, try divide i
again.
edit: think easiest way use do while
-loop. calculate divisor, have multiply i
.
gcd = 1 i=2,10,1 while (mod(n1,i).eq.0.and.mod(n2,i).eq.0) n1=n1/i n2=n2/i gcd = gcd * enddo enddo write(*,*) gcd
Comments
Post a Comment