Task: Emirp primes








VB: Option Explicit

Private Const MAX As Long = 5000000
Private Emirps As New Collection
Private CollTemp As New Collection

Sub Main()
Dim t
t = Timer
    FillCollectionOfEmirps
    Debug.Print "At this point : Execution time = " & Timer - t & " seconds."
    Debug.Print "We have a Collection of the " & Emirps.Count & " first Emirps."
    Debug.Print "---------------------------"
    
    Debug.Print "the first 20 emirps: "; ExtractEmirps(1, 20)
    
    Debug.Print "all emirps between 7,700 and 8,000: "; ExtractEmirps(7700, 8000, True)
    
    Debug.Print "the 10,000th emirp: "; ExtractEmirps(10000, 10000)
  End Sub

Private Function ExtractEmirps(First As Long, Last As Long, Optional Value = False) As String
Dim temp$, i As Long, e
    If First = Last Then
        ExtractEmirps = Emirps(First)
    Else
        If Not Value Then
            For i = First To Last
                temp = temp & ", " & Emirps(i)
            Next
        Else
            For Each e In Emirps
                If e > First And e < Last Then
                    temp = temp & ", " & e
                End If
                If e = Last Then Exit For
            Next e
        End If
        ExtractEmirps = Mid(temp, 3)
    End If
End Function

Private Sub FillCollectionOfEmirps()
Dim Primes() As Long, e, i As Long
    Primes = Atkin
    For i = LBound(Primes) To UBound(Primes)
        CollTemp.Add Primes(i), CStr(Primes(i))
    Next i
    For Each e In CollTemp
        If IsEmirp(e) Then Emirps.Add e
    Next
End Sub

Private Function Atkin() As Long()
Dim MyBool() As Boolean
Dim SQRT_MAX As Long, i&, j&, N&, cpt&, MAX_TEMP As Long, temp() As Long
    ReDim MyBool(MAX)
    SQRT_MAX = Sqr(MAX) + 1
    MAX_TEMP = Sqr(MAX / 4) + 1
    For i = 1 To MAX_TEMP
        For j = 1 To SQRT_MAX
            N = 4 * i * i + j * j
            If N <= MAX And (N Mod 12 = 1 Or N Mod 12 = 5) Then
                MyBool(N) = True
            End If
        Next j
    Next i
    MAX_TEMP = Sqr(MAX / 3) + 1
    For i = 1 To MAX_TEMP
        For j = 1 To SQRT_MAX
            N = 3 * i * i + j * j
            If N <= MAX And N Mod 12 = 7 Then
                MyBool(N) = True
            End If
        Next j
    Next i
    For i = 1 To SQRT_MAX
        For j = 1 To SQRT_MAX
            N = 3 * i * i - j * j
            If i > j And N <= MAX And N Mod 12 = 11 Then
                MyBool(N) = True
            End If
        Next j
    Next i
    For i = 5 To SQRT_MAX Step 2
        If MyBool(i) Then
            For j = i * i To MAX Step i
                MyBool(j) = False
            Next
        End If
    Next
    ReDim temp(MAX / 2)
    temp(0) = 2: temp(1) = 3: cpt = 2
    For i = 5 To MAX Step 2
        If MyBool(i) Then temp(cpt) = i: cpt = cpt + 1
    Next
    ReDim Preserve temp(cpt - 1)
    Atkin = temp
End Function

Private Function IsEmirp(N) As Boolean
Dim a As String, b As String
    a = StrReverse(CStr(N)): b = CStr(N)
    If a <> b Then
        On Error Resume Next
        CollTemp.Add a, a
        If Err.Number > 0 Then
            IsEmirp = True
        Else
            CollTemp.Remove a
        End If
        On Error GoTo 0
    End If
End Function



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