Primorial primes

From Rosetta Code
Revision as of 20:05, 14 August 2022 by Thundergnat (talk | contribs) (remove task markup so it doesn't get indexed)
Please do not add new examples to this page.

Near exact duplicate of task Sequence of primorial primes.

Please merge examples here with that page pending deletion. At least one language example exists here but not there.

Definition

A primorial prime is a prime number that is one less or one more than a primorial.

In other words a non-negative integer n corresponds to a primorial prime if either pn# - 1 or pn# + 1 is prime where pn# denotes the product of the first n primes.

Examples

4 corresponds to the primorial prime p4# + 1 = 2 x 3 x 5 x 7 + 1 = 211.

6 corresponds to the primorial prime p6# - 1 = 210 x 11 x 13 - 1 = 30029.

Task

Find and show here the first 12 primorial primes. As well as the prime itself show the primorial number pn to which it corresponds and whether 1 is to be added or subtracted.

As p0# (by convention) is 1 and p1# is 2, start counting from p0#.

Stretch

If your language supports arbitrary sized integers, do the same for at least the next 12 primorial primes.

As it can take a long time to demonstrate that a large number (above say 2^64) is definitely prime, you may instead use a function which shows that a number is probably prime to a reasonable degree of certainty. Most 'big integer' libraries have such a function.

If a number has more than 40 digits, do not show the full number. Show instead the first 20 and the last 20 digits and how many digits in total the number has.

References
Related task



Factor

Works with: Factor version 0.99 2022-04-03

<lang factor>USING: formatting kernel math math.factorials math.parser math.primes sequences ;

.p ( n i sgn p -- )
   [ >dec "p" "#" surround ] 2dip
   "%2d:%6s %s 1 = %d\n" printf ;

[let

   1 0 :> ( i! p! )
   [ i 12 <= ] [
       p primorial 1 - :> a
       a 2 + :> b
       a prime? [ i p "-" a .p i 1 + i! ] when
       b prime? [ i p "+" b .p i 1 + i! ] when
       p 1 + p!
   ] while

]</lang>

Output:
 1:   p0# + 1 = 2
 2:   p1# + 1 = 3
 3:   p2# - 1 = 5
 4:   p2# + 1 = 7
 5:   p3# - 1 = 29
 6:   p3# + 1 = 31
 7:   p4# + 1 = 211
 8:   p5# - 1 = 2309
 9:   p5# + 1 = 2311
10:   p6# - 1 = 30029
11:  p11# + 1 = 200560490131
12:  p13# - 1 = 304250263527209

J

Compare with the j factorial prime implementation. Columns here are primorial prime number cardinal number, primorial prime, primorial index number, and offset from the corresponding primorial to the prime: <lang J> P=: 1,*/\ p:i.15 NB. primorials represented as fixed width 64 bit integers

  (,.~ #\)(,. (-{&P)/"1) (,. P I. <:) /:~(#~ 1 p: ]) ,P+/1 _1
1               2  0  1
2               3  1  1
3               5  2 _1
4               7  2  1
5              29  3 _1
6              31  3  1
7             211  4  1
8            2309  5 _1
9            2311  5  1

10 30029 6 _1 11 200560490131 11 1 12 304250263527209 13 _1</lang>