Task: Find largest left truncatable prime in a given base

C: #include <stdio.h>
#include <gmp.h>

typedef unsigned long ulong;

ulong 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};

#define MAX_STACK 128
mpz_t tens[MAX_STACK], value[MAX_STACK], answer;

ulong base, seen_depth;

void add_digit(ulong i)
{
	ulong d;
	for (d = 1; d < base; d++) {
		mpz_set(value[i], value[i-1]);
		mpz_addmul_ui(value[i], tens[i], d);
		if (!mpz_probab_prime_p(value[i], 1)) continue;

		if (i > seen_depth ||
			(i == seen_depth && mpz_cmp(value[i], answer) == 1))
		{
			if (!mpz_probab_prime_p(value[i], 50)) continue;

			mpz_set(answer, value[i]);
			seen_depth = i;
			gmp_fprintf(stderr, "\tb=%lu d=%2lu | %Zd\n", base, i, answer);
		}

		add_digit(i+1);
	}
}

void do_base()
{
	ulong i;
	mpz_set_ui(answer, 0);
	mpz_set_ui(tens[0], 1);
	for (i = 1; i < MAX_STACK; i++)
		mpz_mul_ui(tens[i], tens[i-1], base);

	for (seen_depth = i = 0; small_primes[i] < base; i++) {
		fprintf(stderr, "\tb=%lu digit %lu\n", base, small_primes[i]);
		mpz_set_ui(value[0], small_primes[i]);
		add_digit(1);
	}
	gmp_printf("%d: %Zd\n", base, answer);
}

int main(void)
{
	ulong i;
	for (i = 0; i < MAX_STACK; i++) {
		mpz_init_set_ui(tens[i], 0);
		mpz_init_set_ui(value[i], 0);
	}
	mpz_init_set_ui(answer, 0);

	for (base = 22; base < 30; base++) do_base();

	return 0;
}










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