Task: Emirp primes







Python: from __future__ import print_function
from prime_decomposition import primes, is_prime
from heapq import *
from itertools import islice

def emirp():
    largest = set()
    emirps = []
    heapify(emirps)
    for pr in primes():
        while emirps and pr > emirps[0]:
            yield heappop(emirps)
        if pr in largest:
            yield pr
        else:
            rp = int(str(pr)[::-1])
            if rp > pr and is_prime(rp):
                heappush(emirps, pr)
                largest.add(rp)

print('First 20:\n  ', list(islice(emirp(), 20)))
print('Between 7700 and 8000:\n  [', end='')
for pr in emirp():
    if pr >= 8000: break
    if pr >= 7700: print(pr, end=', ')
print(']')
print('10000th:\n  ', list(islice(emirp(), 10000-1, 10000)))




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