FORTRAN logic notproblem -
if use function once, works properly. if make loop down below, 4 lines of commented code, code malfunctions. can't figure out why return t or f every other number after initial value.
asterisks in parenthesis of write , read doesn't show here reason.
program prime integer :: n = 0, = 1,x = 0 logical :: ip write (*,*) "enter number:" read (*,*) n !do while ( n < 1000) ip = isprime(n) write (*,*) ip, n !n = n + 1 !end read(*,*) x contains function isprime(n) logical :: isprime integer, intent(in) :: n isprime = .true. if (n == 2) write (*,*) n else while (i <= (n/2)) = + 2 if (mod(n,i) == 0) isprime = .false. end if end end if return end function isprime end program prime
you're forgetting reset i 1 during each call isprime.
the first time isprime called, i=1 top of program main. however, i incremented during first isprime call other 1 second call starts i/=0.
note because isprime contained within program main, isprime inherits i program main.
i'm bound remind use implicit none everywhere avoid other errors, although it's not problem in case.
Comments
Post a Comment