Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error when using "X != 0" in Fortran

I have a problem with my Fortran program that does nothing more than calculating a prime factorization (or should do). That's the error:

C:\MinGW\Fortran>gfortran aufg3.f90
aufg3.f90:15.15:

    if (prim(i) != 0 .and. modulo(n, prim(i)) == 0) then
               1
Error: Missing ')' in statement at or before (1)
aufg3.f90:19.7:

    end if
       1
Error: Expecting END DO statement at (1)
aufg3.f90:34.13:

  if (prim(i) != 0) then
             1
Error: Missing ')' in statement at or before (1)
aufg3.f90:38.5:

  end if
     1
Error: Expecting END DO statement at (1)

I tried everything, but totally have no idea what could be wrong. Thanks for your help. Here is the code:

program aufg3
    implicit none
    integer :: n, i
    integer, allocatable, dimension(:) :: prim
    do
        print *, 'Bitte natürliche Zahl eingeben, "0" für Abbruch: '
        read *, n
        if (n == 0) exit
        allocate(prim(2:n))
        call era(prim, n)
        print *, n, ' = 1'
        do
            if (n == 1) exit
            do i = 2, n
                if (prim(i) != 0 .and. modulo(n, prim(i)) == 0) then
                    print *, ' * ', prim(i)
                    n = n / prim(i)
                    exit
                end if
            end do
        end do
        deallocate(prim)
    end do
end program

subroutine era(prim, m) integer, intent(in) :: m integer, dimension(2:m) :: prim integer :: i, j do i = 2, m prim(i) = i end do do i = 2, integer(sqrt(real(m))) if (prim(i) != 0) then do j = i*i, m, i prim(j) = 0 end do end if end do end subroutine

like image 822
DonFuchs Avatar asked Sep 18 '15 16:09

DonFuchs


Video Answer


2 Answers

Well, this is Fortran and ! denotes a comment. So the compiler actually sees

if (prim(i) 

which is no valid statement. The error message you see reflects that.

"Not equal" in Fortran is /= or .ne.:

 if (prim(i) /= 0 .and. modulo(n, prim(i)) == 0) then

and, later on:

if (prim(i) /= 0) then
like image 162
Alexander Vogt Avatar answered Sep 28 '22 00:09

Alexander Vogt


You're using the wrong notation for 'not equal to'. Fortran syntax is /= or .NE..

So you should be using:

if (prim(i) /= 0 .and. modulo(n, prim(i)) == 0) then

and

if (prim(i) /= 0) then

Furthermore, your syntax of integer(sqrt(real(m))) is incorrect, perhaps you mean NINT(sqrt(real(m)))?

like image 32
Ross Avatar answered Sep 27 '22 22:09

Ross