Task: Emirp primes






PHP: <?php

function is_prime($n) {
    if ($n <= 3) {
        return $n > 1;
    } elseif (($n % 2 == 0) or ($n % 3 == 0)) {
        return false;
    }
    $i = 5;
    while ($i * $i <= $n) {
        if ($n % $i == 0) {
            return false;
        }
        $i += 2;
        if ($n % $i == 0) {
            return false;
        }
        $i += 4;
    }
    return true;
}

function is_emirp($n) {
    $r = (int) strrev((string) $n);
    return (($r != $n) and is_prime($r) and is_prime($n));
}

$c = $x = 0;
$first20 = $between = '';
do {
    $x++;
    if (is_emirp($x)) {
        $c++;
        if ($c <= 20) {
            $first20 .= $x . ' ';
        }
        if (7700 <= $x and $x <= 8000) {
            $between .= $x . ' ';
        }
    }
} while ($c < 10000);

echo
    'First twenty emirps :', PHP_EOL, $first20, PHP_EOL,
    'Emirps between 7,700 and 8,000 :', PHP_EOL, $between, PHP_EOL,
    'The 10,000th emirp :', PHP_EOL, $x, PHP_EOL;





Fortran:       MODULE BAG	
       INTEGER MSG	
       INTEGER PF16LIMIT,PF32LIMIT,NP			
       PARAMETER (PF16LIMIT = 3512, PF32LIMIT = 4793)	
       INTEGER*2 PRIME16(PF16LIMIT)			
       INTEGER*4 PRIME32(PF16LIMIT + 1:PF32LIMIT)	
       DATA NP,PRIME16(1),PRIME16(2)/2,2,3/	
       INTEGER NGP,NNP,NIP	
       DATA NGP,NNP,NIP/3*0/	
       CONTAINS		
        RECURSIVE INTEGER FUNCTION GETPRIME(I)	
Chooses from amongst two arrays, of sizes known from previous work.
         INTEGER I		
         INTEGER P		
         INTEGER MP		
          NGP = NGP + 1		
          IF (I.LE.0) THEN	
            GETPRIME = -666		
          ELSE IF (I.LE.NP) THEN	
            IF (I.LE.PF16LIMIT) THEN		
              GETPRIME = PRIME16(I)		
             ELSE				
              GETPRIME = PRIME32(I)		
            END IF			
          ELSE IF (I.LE.PF32LIMIT) THEN	
            MP = NP			
            P = GETPRIME(NP)		
   10       P = NEXTPRIME(P)		
            MP = MP + 1			
            IF (MP.LT.I) GO TO 10	
            GETPRIME = P		
           ELSE			
            WRITE (MSG,*) "Hic
            STOP "Too far..."		
          END IF		
        END FUNCTION GETPRIME	

        SUBROUTINE STASHPRIME(P)	
         INTEGER P	
          NP = NP + 1		
          IF (NP.LE.PF16LIMIT) THEN	
            PRIME16(NP) = P			
          ELSE IF (NP.LE.PF32LIMIT) THEN
            PRIME32(NP) = P			
          ELSE				
            STOP "Stash overflow
          END IF			
        END SUBROUTINE STASHPRIME	

        INTEGER FUNCTION FINDPRIME(IT)	
         INTEGER IT	
         INTEGER L,R,P	
          L = 0		
          R = NP + 1	
    1     P = (R - L)/2		
          IF (P.LE.0) THEN	
            FINDPRIME = -L		
            RETURN			
          END IF		
          P = P + L		
          IF (IT - GETPRIME(P)) 2,4,3	
    2     R = P; GO TO 1	
    3     L = P; GO TO 1	
    4     FINDPRIME = P		
        END FUNCTION FINDPRIME	

        RECURSIVE INTEGER FUNCTION NEXTPRIME(P)	
Checks the stash in PRIME in the hope of finding the next prime directly, otherwise advances from P.
Collates a stash of primes in PRIME16 and PRIME32, advancing NP from 2 to PF32LIMIT as it goes.
         INTEGER P	
         INTEGER PI	
         INTEGER IT	
          NNP = NNP + 1	
          IF (P.LE.1) THEN	
            PI = 2		
           ELSE			
            IT = (ABS(FINDPRIME(P)))	
            IF (IT.LT.NP) THEN		
              PI = GETPRIME(IT + 1)	
             ELSE	
              IF (NP.LT.PF32LIMIT) THEN	
                PI = GETPRIME(IT)	
               ELSE			
                PI = P			
                IF (MOD(PI,2).EQ.0) PI = PI - 1	
              END IF			
              DO WHILE (PI.LE.P)	
   11           PI = PI + 2			
                IF (.NOT.ISPRIME(PI)) GO TO 11	
                IF (IT.EQ.NP .AND. IT.LT.PF32LIMIT) THEN	
                  CALL STASHPRIME(PI)		
                  IT = IT + 1			
                END IF			
              END DO		
            END IF		
          END IF		
          NEXTPRIME = PI	
        END FUNCTION NEXTPRIME	

        RECURSIVE LOGICAL FUNCTION ISPRIME(N)	
Crunches up to SQRT(N), and at worst needs to be able to reach Prime(4793) = 46349; greater than SQRT(2147483647) = 46340·95...
         INTEGER N	
         INTEGER I,F,Q	
          NIP = NIP + 1	
          IF (N.LT.2) THEN	
            ISPRIME = .FALSE.	
          ELSE			
            ISPRIME = .FALSE.	
            I = 1		
   10       F = GETPRIME(I)	
            Q = N/F		
            IF (Q .GE. F) THEN	
              IF (Q*F .EQ. N) RETURN	
              I = I + 1		
              GO TO 10		
            END IF		
            ISPRIME = .TRUE.	
          END IF		
        END FUNCTION ISPRIME	

        INTEGER FUNCTION ESREVER(IT,BASE)	
         INTEGER IT	
         INTEGER BASE	
         INTEGER N,R	
          IF (BASE.LE.1) STOP "Base 2 at least
          N = IT	
          R = 0		
          DO WHILE(N.GT.0)	
            R = R*BASE + MOD(N,BASE)	
            N = N/BASE			
          END DO		
          ESREVER = R		
        END FUNCTION ESREVER	

        SUBROUTINE EMIRP(BASE,N1,N2,I1,I2)	
         INTEGER BASE	
         INTEGER N1,N2	
         INTEGER I1,I2	
         INTEGER N	
         INTEGER P,R	
          WRITE (MSG,1) N1,N2,BASE,I1,I2	
    1     FORMAT ("Show the first ",I0," to ",I0,	
     &     " emirP numbers (base ",I0,") between ",I0," and ",I0)	
          N = 0		
          P = I1 - 1	
Chase another emirP.
   10     P = NEXTPRIME(P)		
          IF (P.LT.I1) GO TO 10		
          IF (P.GT.I2) GO TO 900	
          R = ESREVER(P,BASE)		
          IF (P .EQ. R) GO TO 10	
          IF (.NOT.ISPRIME(R)) GO TO 10	
          N = N + 1			
c          if (mod(n,100) .eq. 0) then
c            write (6,66) N,P,R,NP,NGP,NNP,NIP
c   66       format ("N=",I5,",p=",I6,",R=",I6,",NP=",I6,3I12)
c          end if
          IF (N.GE.N1) WRITE (6,*) P,R	
          IF (N.LT.N2) GO TO 10		
Closedown.
  900     WRITE (MSG,901) NP,GETPRIME(NP)	
  901     FORMAT ("Stashed up to Prime(",I0,") = ",I0,/)
        END SUBROUTINE EMIRP	
      END MODULE BAG	

      PROGRAM POKE	
      USE BAG		
      MSG = 6		

      CALL EMIRP(10,    1,   20,   1,   1000)	
      CALL EMIRP(10,    1,   28,7700,   8000)	
      CALL EMIRP(10,10000,10000,   1,1000000)	

      END