sergih123
sergih123

Reputation: 75

Compiler won't calculate variable unless I print it""

So apparently, depending in wether i tell the program to print the variable i, or not, I get different results that should not have anything to do with wether i print it our or not.

 PROGRAM hello
IMPLICIT NONE
integer :: n,i, mini
logical :: leave = .false.
read*, n
print*, is_prime(n)   

!!---------------------------------------------------------------------

do i=n, n/2, -1
    print*, "I= ", i                !!if you comment out this line, the result will be different than if you were to keep it, try it out yourselves
    if(is_prime(i)) then
        mini = i
    end if
end do

print*, "the lowest prime number between your number and its half is: ", mini

!!----------------------------------------------------------

CONTAINS

logical function is_prime(n)
    integer::n,i

    do i=2,n
        if(mod(n,i) == 0 .and. (i/=1 .and. i/=n) ) then
            is_prime = .false.
        elseif(mod(n,i) /=0 .and. i == n-1 .and. is_prime .neqv. .false.) then
            is_prime = .true.
        end if
    end do
    return
end function
END PROGRAM

So if you were to comment out the line I pointed out, the result of "mini" will be different than if you were to keep it, as I said. I'm fairly new at fortran so I don't know wether I'm doing something wrong, or if this has something to do with the compiler, but it seems really weird to me that putting a print*, line would in any way change the value of the variabe, and that's what seems to happen.

For example if you try it yourselve, the output of mini when the print line is in, is for exaple,, typing in 48, is 29, which is right, it's the minimum prime number between 48 and ts half, but when you tipe in 48 and the famous print line is commented out, the output will be -2, instead of 29. Any of you know why this happenes?

Upvotes: 1

Views: 82

Answers (1)

Pierre de Buyl
Pierre de Buyl

Reputation: 7293

@francescalus is right, the logic of is_prime is wrong.

You can tell by checking the first result (the print *, is_prime(n)) of the program.

Below, a version with a correct is_prime. I first assign .true. to the result and invalidate it to .false. when the test is true.

PROGRAM hello
  IMPLICIT NONE
  integer :: n,i, mini

  read*, n
  print*, is_prime(n)

!!---------------------------------------------------------------------

  do i=n, n/2, -1
     print*, "I= ", i
     if(is_prime(i)) then
        mini = i
     end if
  end do

  print*, "the lowest prime number between your number and its half is: ", mini

!!----------------------------------------------------------

CONTAINS

  logical function is_prime(n)
    integer, intent(in) :: n
    integer :: i

    is_prime = .true.
    do i=2,n
       if(mod(n,i) == 0 .and. (i/=1 .and. i/=n) ) then
          is_prime = .false.
       end if
    end do
  end function is_prime

END PROGRAM

EDIT: I should add that the issue with the influence of the print statement comes up from time to time. When it arises, it points to a flaw in the logic of the code that then becomes sensitive to situations of ill-defined results.

Upvotes: 4

Related Questions