Task: Find largest left truncatable prime in a given base



C#: using Mpir.NET;  
using System;   
using System.Collections.Generic;
class MaxLftTrP_B   
{
    static void Main()
    {
        mpz_t p; var sw = System.Diagnostics.Stopwatch.StartNew(); L(3);
        for (uint b = 3; b < 13; b++)
        {
            sw.Restart(); p = L(b);
            Console.WriteLine("{0} {1,2} {2}", sw.Elapsed, b, p);
        }
        Console.Read();
    }

    static mpz_t L(uint b)
    {
        var p = new List<mpz_t>(); mpz_t np = 0;
        while ((np = nxtP(np)) < b) p.Add(np);
        int i0 = 0, i = 0, i1 = p.Count - 1; mpz_t n0 = b, n, n1 = b * (b - 1);
        for (; i < p.Count; n0 *= b, n1 *= b, i0 = i1 + 1, i1 = p.Count - 1)
            for (n = n0; n <= n1; n += n0)
                for (i = i0; i <= i1; i++)
                    if (mpir.mpz_probab_prime_p(np = n + p[i], 15) > 0) p.Add(np);
        return p[p.Count - 1];
    }

    static mpz_t nxtP(mpz_t n) { mpz_t p = 0; mpir.mpz_nextprime(p, n); return p; }
}








Fortran:       USE PRIMEBAG	
Calculates the largest "left-truncatable" digit sequence that is a prime number, in various bases.
      INTEGER LBASE,MANY,ENUFF	
      PARAMETER (LBASE = 13, MANY = 66666, ENUFF = 66)
      INTEGER NS,START(LBASE)	
      INTEGER NH,LH		
      INTEGER N,HORDEN(MANY)		
      INTEGER*1 HORDED(ENUFF,MANY)	
      INTEGER B,D,DB	
      INTEGER L		
      INTEGER P		
      INTEGER I		

      MSG = 6	
      IF (.NOT.GRASPPRIMEBAG(66)) STOP "Gan't grab my file
      NS = 0	
      P = 1	
    1 P = NEXTPRIME(P)	
      IF (P.LE.LBASE) THEN	
        NS = NS + 1		
        START(NS) = P		
        GO TO 1			
      END IF		
      WRITE (MSG,2) LBASE,NS,START(1:NS)	
    2 FORMAT ("Working in bases 3 to ",I0," there are ",I0,	
     * " single-digit primes: ",666(I0:", "))	
      WRITE (MSG,3)		
    3 FORMAT (/"Base Digits Count Max. Value = (in base)")

   10 DO B = 3,LBASE	
        NH = 0			
        DO I = 1,NS		
          IF (START(I).GE.B) EXIT	
          NH = NH + 1			
          HORDEN(NH) = START(I)		
          HORDED(1,NH) = START(I)	
        END DO			
        L = 0	
        DB = 1	

   20   L = L + 1	
        IF (L.GE.ENUFF) STOP "Too many digits
        DB = DB*B	
        IF (DB.LE.0) GO TO 29	
        LH = NH		
        DO I = 1,LH	
          N = HORDEN(I)	
          DO D = 1,B - 1	
            P = D*DB + N		
            IF (P.LE.0) GO TO 29	
            IF (ISPRIME(P)) THEN	
              IF (NH.GE.MANY) STOP "Too many sequences
              NH = NH + 1			
              HORDEN(NH) = P			
              HORDED(1:L,NH) = HORDED(1:L,I)	
              HORDED(L + 1,NH) = D		
            END IF			
          END DO		
        END DO	

        N = NH - LH		
        IF (N.GT.0) THEN	
          DO I = 1,MIN(LH,N)		
            HORDEN(I) = HORDEN(NH)		
            HORDED(1:L + 1,I) = HORDED(1:L + 1,NH)	
            NH = NH - 1				
          END DO			
          NH = N			
          LH = NH			
          GO TO 20			
        END IF			
        GO TO 30		
   29   WRITE (MSG,28) B,L,NH,DB,P	
   28   FORMAT (I4,I7,I6,28X,"Integer overflow
        CYCLE			

   30   I = MAXLOC(HORDEN(1:NH),DIM = 1)	
        WRITE (MSG,31) B,L,NH,HORDEN(I),HORDED(L:1:-1,I)	
   31   FORMAT (I4,I7,I6,I11," = "666(I0:"."))	

      END DO		
      END