Task: Sequence of primorial primes


C++: #include <cstdint>
#include <iostream>
#include <sstream>
#include <gmpxx.h>

typedef mpz_class integer;

bool is_probably_prime(const integer& n) {
    return mpz_probab_prime_p(n.get_mpz_t(), 25) != 0;
}

bool is_prime(unsigned int n) {
    if (n < 2)
        return false;
    if (n % 2 == 0)
        return n == 2;
    if (n % 3 == 0)
        return n == 3;
    for (unsigned int p = 5; p * p <= n; p += 4) {
        if (n % p == 0)
            return false;
        p += 2;
        if (n % p == 0)
            return false;
    }
    return true;
}

int main() {
    const unsigned int max = 20;
    integer primorial = 1;
    for (unsigned int p = 0, count = 0, index = 0; count < max; ++p) {
        if (!is_prime(p))
            continue;
        primorial *= p;
        ++index;
        if (is_probably_prime(primorial - 1) || is_probably_prime(primorial + 1)) {
            if (count > 0)
                std::cout << ' ';
            std::cout << index;
            ++count;
        }
    }
    std::cout << '\n';
    return 0;
}









Fortran:       PROGRAM PRIMORIALP	
      USE PRIMEBAG		
      USE BIGNUMBERS		
      TYPE(BIGNUM) B		
      INTEGER MAXF		
      PARAMETER (MAXF = 18000000)	
      INTEGER I			
      INTEGER FU,FD		
      INTEGER NHIT,HIT(666)	
      CHARACTER*4 WOT		
      CHARACTER*66 ALINE	
      REAL T0,T1		
      MSG = 6	
      WRITE (MSG,1) BIGLIMIT,BIGBASE,HUGE(I)	
    1 FORMAT ('Calculates primorial "primes"',/,
     1 "A primorial prime is a value N such that",/,
     2 "    Primorial(N) - 1 is prime, OR",/,
     3 "    Primorial(N) + 1 is prime, or both.",/,
     4 "and Primorial(N) is the product of the first N prime numbers.",/
     5 "Working with up to ",I0," digits in base ",I0,"."/
     6 "The integer limit is ",I0,/)

c      CALL PREPARE PRIMES	
      IF (.NOT.GRASPPRIMEBAG(66)) STOP "Gan't grab my file
      WRITE (MSG,2)
    2 FORMAT ("Primorial#",3X,"Approx.",8X," -1 Factor +1 Factor Hit")

Commence prime mashing.
  100 NHIT = 0		
      B.LAST = 1	
      B.DIGIT(1) = 1	
      CALL CPU_TIME(T0)	
      DO I = 1,30	
        CALL BIGMULTN(B,PRIME(I))	
c        WRITE (MSG,101) I,PRIME(I),I,B.DIGIT(B.LAST:1:-1)	
  101   FORMAT ("Prime(",I0,") = ",I0,", Primorial(",I0,") = ",	
     1   I0,9I<BIGORDER>.<BIGORDER>,/,(10I<BIGORDER>.<BIGORDER>))	
        FU = -1		
        FD = -1		
        CALL BIGADDN(B,+1)	
        FU = BIGFACTOR(B,MAXF)	
        CALL BIGADDN(B,-2)	
        IF (FU.NE.1) FD = BIGFACTOR(B,MAXF)	
        IF (FU.EQ.1 .OR. FD.EQ.1) THEN	
          WOT = "Yes
          NHIT = NHIT + 1			
          HIT(NHIT) = I				
        ELSE IF (FU.GT.1 .AND. FD.GT.1) THEN	
          WOT = "No."				
        ELSE				
          WOT = "?"				
        END IF				
        CALL BIGADDN(B,1)		
        WRITE (ALINE,102) I,BIGVALUE(B),FD,FU,WOT	
  102   FORMAT (I10,1PE18.10,I10,I10,1X,A)	
        IF (FD.EQ.-1) ALINE(37:38) = ""		
        IF (FD.EQ. 0) ALINE(38:38) = "?"	
        IF (FU.EQ. 0) ALINE(48:48) = "?"	
        WRITE (MSG,"(A)") ALINE		
      END DO		
      CALL CPU_TIME(T1)	

Cast forth some pearls.
      WRITE (MSG,201) HIT(1:NHIT)	
  201 FORMAT (/,"Hit list: ",I0,666(",",I0:))	
      WRITE (MSG,*) "CPU time:",T1 - T0	
      END