FUNCTION isprime(k)
  IMPLICIT NONE
  INTEGER, INTENT (in) :: k
  LOGICAL :: isprime

  INTEGER :: i

  isprime = .TRUE.

  DO i=2,k-1
     IF ( MOD(k,i) == 0 ) THEN
        isprime = .FALSE.
        EXIT
     ENDIF
  ENDDO

END FUNCTION isprime

FUNCTION f(n)
  IMPLICIT NONE
  INTEGER, INTENT (out) :: n
  INTEGER :: f

  INTERFACE 
     FUNCTION isprime(k)
       IMPLICIT NONE
       INTEGER, INTENT (in) :: k
       LOGICAL :: isprime
     END FUNCTION isprime
  END INTERFACE

  INTEGER :: p, i

  f = 0
  
  OPEN(33, file="rndint", action="read", status="old")
  READ(33,*) p

  DO i=1,p
     READ(33,*) n
     IF (isprime(n)) THEN
        f = i+1
        EXIT
     END IF
  END DO

  CLOSE(33)
  
END FUNCTION f


PROGRAM example
  IMPLICIT NONE

  INTERFACE
     FUNCTION f(n)
       IMPLICIT NONE
       INTEGER, INTENT (out) :: n
       INTEGER :: f
     END FUNCTION f
  END INTERFACE
  INTEGER :: n, d
  
  d = f(n)
  PRINT *, "O ",  n, "sthn ", d, " grammh"
END PROGRAM example
