Task: Find largest left truncatable prime in a given base





Java: import java.math.BigInteger;
import java.util.*;

class LeftTruncatablePrime
{
  private static List<BigInteger> getNextLeftTruncatablePrimes(BigInteger n, int radix, int millerRabinCertainty)
  {
    List<BigInteger> probablePrimes = new ArrayList<BigInteger>();
    String baseString = n.equals(BigInteger.ZERO) ? "" : n.toString(radix);
    for (int i = 1; i < radix; i++)
    {
      BigInteger p = new BigInteger(Integer.toString(i, radix) + baseString, radix);
      if (p.isProbablePrime(millerRabinCertainty))
        probablePrimes.add(p);
    }
    return probablePrimes;
  }
  
  public static BigInteger getLargestLeftTruncatablePrime(int radix, int millerRabinCertainty)
  {
    List<BigInteger> lastList = null;
    List<BigInteger> list = getNextLeftTruncatablePrimes(BigInteger.ZERO, radix, millerRabinCertainty);
    while (!list.isEmpty())
    {
      lastList = list;
      list = new ArrayList<BigInteger>();
      for (BigInteger n : lastList)
        list.addAll(getNextLeftTruncatablePrimes(n, radix, millerRabinCertainty));
    }
    if (lastList == null)
      return null;
    Collections.sort(lastList);
    return lastList.get(lastList.size() - 1);
  }
  
  public static void main(String[] args)
  {
    if (args.length != 2) {
      System.err.println("There must be exactly two command line arguments.");
      return;
    }
    int maxRadix;
    try {
      maxRadix = Integer.parseInt(args[0]);
      if (maxRadix < 3) throw new NumberFormatException(); 
    } catch (NumberFormatException e) {
      System.err.println("Radix must be an integer greater than 2.");
      return;
    }
    int millerRabinCertainty;
    try {
      millerRabinCertainty = Integer.parseInt(args[1]);
    } catch (NumberFormatException e) {
      System.err.println("Miiller-Rabin Certainty must be an integer.");
      return;
    }  
    for (int radix = 3; radix <= maxRadix; radix++)
    {
      BigInteger largest = getLargestLeftTruncatablePrime(radix, millerRabinCertainty);
      System.out.print("n=" + radix + ": ");
      if (largest == null)
        System.out.println("No left-truncatable prime");
      else
        System.out.println(largest + " (in base " + radix + "): " + largest.toString(radix));
    }
  }
  
}






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