Task: Find largest left truncatable prime in a given base


C++: #include <gmpxx.h>

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <vector>

using big_int = mpz_class;

const unsigned int small_primes[] = {2,  3,  5,  7,  11, 13, 17, 19, 23,
                                     29, 31, 37, 41, 43, 47, 53, 59, 61,
                                     67, 71, 73, 79, 83, 89, 97};

bool is_probably_prime(const big_int& n, int reps) {
    return mpz_probab_prime_p(n.get_mpz_t(), reps) != 0;
}

big_int largest_left_truncatable_prime(unsigned int base) {
    std::vector<big_int> powers = {1};
    std::vector<big_int> value = {0};
    big_int result = 0;

    std::function<void(unsigned int)> add_digit = [&](unsigned int i) {
        if (i == value.size()) {
            value.resize(i + 1);
            powers.push_back(base * powers.back());
        }
        for (unsigned int d = 1; d < base; ++d) {
            value[i] = value[i - 1] + powers[i] * d;
            if (!is_probably_prime(value[i], 1))
                continue;
            if (value[i] > result) {
                if (!is_probably_prime(value[i], 50))
                    continue;
                result = value[i];
            }
            add_digit(i + 1);
        }
    };

    for (unsigned int i = 0; small_primes[i] < base; ++i) {
        value[0] = small_primes[i];
        add_digit(1);
    }
    return result;
}

int main() {
    for (unsigned int base = 3; base < 18; ++base) {
        std::cout << base << ": " << largest_left_truncatable_prime(base)
                  << '\n';
    }
    for (unsigned int base = 19; base < 32; base += 2) {
        std::cout << base << ": " << largest_left_truncatable_prime(base)
                  << '\n';
    }
}









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