Task: Find largest left truncatable prime in a given base







Python: import random

def is_probable_prime(n,k):
    
    if n==0 or n==1:
        return False
    if n==2:
        return True
    if n % 2 == 0:
        return False
    s = 0
    d = n-1

    while True:
        quotient, remainder = divmod(d, 2)
        if remainder == 1:
            break
        s += 1
        d = quotient

    def try_composite(a):
        if pow(a, d, n) == 1:
            return False
        for i in range(s):
            if pow(a, 2**i * d, n) == n-1:
                return False
        return True 
 
    for i in range(k):
        a = random.randrange(2, n)
        if try_composite(a):
            return False
 
    return True 
    
    
def largest_left_truncatable_prime(base):    
    radix = 0
    candidates = [0]
    while True:
        new_candidates=[]
        multiplier = base**radix
        for i in range(1,base):
            new_candidates += [x+i*multiplier for x in candidates if is_probable_prime(x+i*multiplier,30)]
        if len(new_candidates)==0:
            return max(candidates)
        candidates = new_candidates
        radix += 1

for b in range(3,24):
    print("%d:%d\n" % (b,largest_left_truncatable_prime(b)))




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