Quad-power prime seeds: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Added link to the Penta-power prime seeds task.)
Line 15: Line 15:


;See also
;See also
;*[[Penta-power prime seeds|Task: Penta-power prime seeds]]
;*[[oeis:A219117|A219117 - Numbers n such that n^1+n+1, n^2+n+1, n^3+n+1 and n^4+n+1 are all prime]]
;*[[oeis:A219117|A219117 - Numbers n such that n^1+n+1, n^2+n+1, n^3+n+1 and n^4+n+1 are all prime]]



Revision as of 14:30, 20 August 2022

Quad-power prime seeds is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Generate the sequence of quad-power prime seeds: positive integers n such that:

   n1 + n + 1, n2 + n + 1, n3 + n + 1 and n4 + n + 1 are all prime.


Task
  • Find and display the first fifty quad-power prime seeds.


Stretch
  • Find and display the position and value of first with a value greater than one million, two million, three million.


See also



ALGOL 68

Works with: ALGOL 68G version Any - tested with release 3.0.3 under Windopws

This uses ALGOL 68G's LONG LONG INT during the Miller Rabin testing, under ALGOL 68G version three, the default precision of LONG LONG INT is 72 digits and LONG INT is 128 bit. A sieve is used to (hopefully) quickly eliminate non-prime 2n+1 numbers - Miller Rabin is used for n^2+n+1 etc. that are larger than the sieve. This is about 10 times slower than the equivalent Penta-powwr prime seed program, possibly because even numbers are included and the n+2 test in the Penta-powers eliminates more numbers before the higher powers must be calculated.
Note that to run this with ALGOL 68G under Windows (and probably Linux) a large heap sie must be specified on the command line, e.g. -heap 1024M. <lang algol68>BEGIN # find some Quad power prime seeds, numbers n such that: #

     #      n^p + n + 1 is prime for p = 1, 2, 3, 4                          #
   PR read "primes.incl.a68" PR # include prime utilities                    #
   INT max prime         = 22 000 000;
   # sieve the primes to max prime - 22 000 000 should be enough to allow    #
   # checking primality of 2n+1 up to a little under 11 000 000 which should #
   # be enough for the task                                                  #
   [ 0 : max prime ]BOOL prime;
   FOR i FROM LWB prime TO UPB prime DO prime[ i ] := FALSE OD;
   prime[ 0 ] := prime[ 1 ] := FALSE;
   prime[ 2 ] := TRUE;
   FOR i FROM 3 BY 2 TO UPB prime DO prime[ i ] := TRUE  OD;
   FOR i FROM 4 BY 2 TO UPB prime DO prime[ i ] := FALSE OD;
   FOR i FROM 3 BY 2 TO ENTIER sqrt( UPB prime ) DO
       IF prime[ i ] THEN
           FOR s FROM i * i BY i + i TO UPB prime DO prime[ s ] := FALSE OD
       FI
   OD;
   # returns TRUE if p is (probably) prime, FALSE otherwise                  #
   #         uses the sieve if possible, Miller Rabin otherwise              #
   PROC is prime = ( LONG INT p )BOOL:
        IF p <= max prime
        THEN prime[ SHORTEN p ]
        ELSE is probably prime( p )
        FI;
   # attempt to find the numbers                                             #
   BOOL finished       := FALSE;
   INT  count          := 0;
   INT  next limit     :=  1 000 000;
   INT  last limit      = 10 000 000;
   INT  limit increment = next limit;
   print( ( "First 50 Quad power prime seeds:", newline ) );
   FOR n WHILE NOT finished DO
       IF  INT n1 = n + 1;
           prime[ n + n1 ]
       THEN
           # n^1 + n + 1 is prime                                            #
           LONG INT np := n * n;
           IF is prime( np + n1 ) THEN
               # n^2 + n + 1 is prime                                        #
               IF is prime( ( np *:= n ) + n1 ) THEN
                   # n^3 + n + 1 is prime                                    #
                   IF is prime( ( np * n ) + n1 ) THEN
                       # n^4 + n + 1 is prime - have a suitable number       #
                       count +:= 1;
                       IF n > next limit THEN
                           # found the first element over the next limit     #
                           print( ( newline
                                  , "First element over ", whole( next limit, -10 )
                                  , ": ",                  whole( n,          -10 )
                                  , ", index: ",           whole( count,      -6 )
                                  )
                                );
                           next limit +:= limit increment;
                           finished    := next limit > last limit
                       FI;
                       IF count <= 50 THEN
                           # found one of the first 30 numbers               #
                           print( ( " ", whole( n, -8 ) ) );
                           IF count MOD 10 = 0 THEN print( ( newline ) ) FI
                       FI
                   FI
               FI
           FI
       FI
   OD

END</lang>

Output:
First 50 Quad power prime seeds:
        1        2        5        6       69      131      426     1665     2129     2696
     5250     7929     9689    13545    14154    14286    16434    19760    25739    27809
    29631    36821    41819    46619    48321    59030    60500    61955    62321    73610
    77685    79646    80535    82655    85251    86996    91014    96566    97739   105939
   108240   108681   119754   122436   123164   126489   140636   150480   153179   163070

First element over    1000000:    1009286, index:    141
First element over    2000000:    2015496, index:    234
First element over    3000000:    3005316, index:    319
First element over    4000000:    4004726, index:    383
First element over    5000000:    5023880, index:    452
First element over    6000000:    6000554, index:    514
First element over    7000000:    7047129, index:    567
First element over    8000000:    8005710, index:    601
First element over    9000000:    9055151, index:    645
First element over   10000000:   10023600, index:    701

Factor

Works with: Factor version 0.99 2022-04-03

<lang factor>USING: grouping io kernel lists lists.lazy math math.functions math.primes prettyprint sequences tools.memory.private ;

seed? ( n -- ? )
   { 1 2 3 4 } [ dupd ^ 1 + + prime? ] with all? ;
quads ( -- list )
   1 lfrom [ seed? ] lfilter [ commas ] lmap-lazy ;

"First fifty quad-power prime seeds:" print 50 quads ltake list>array 10 group simple-table.</lang>

Output:
First fifty quad-power prime seeds:
1       2       5       6       69      131     426     1,665   2,129   2,696
5,250   7,929   9,689   13,545  14,154  14,286  16,434  19,760  25,739  27,809
29,631  36,821  41,819  46,619  48,321  59,030  60,500  61,955  62,321  73,610
77,685  79,646  80,535  82,655  85,251  86,996  91,014  96,566  97,739  105,939
108,240 108,681 119,754 122,436 123,164 126,489 140,636 150,480 153,179 163,070

Phix

with javascript_semantics
include mpfr.e
mpz {p,q} = mpz_inits(2)
 
function isQuadPowerPrimeSeed(integer n)
    mpz_set_si(p,n)
    for i=1 to 4 do
        if i>1 then mpz_mul_si(p,p,n) end if
        mpz_add_ui(q,p,n+1)
        if not mpz_prime(q) then return false end if
    end for
    return true
end function
 
sequence qpps = {}
integer n = 1
while length(qpps)<50 do
    if isQuadPowerPrimeSeed(n) then qpps &= n end if
    n += 1
end while
printf(1,"First fifty quad-power prime seeds:\n%s\n",
        {join_by(qpps,1,10," ",fmt:="%,7d")})
 
printf(1,"First quad-power prime seed greater than:\n")
integer m = 1, c = 50
while m<=10 do
    if isQuadPowerPrimeSeed(n) then
        c += 1
        if n > m * 1e6 then
            printf(1," %5s million is %,d (the %s)\n", {ordinal(m,true), n, ordinal(c)})
            m += 1
        end if
    end if
    n += 1
end while
Output:
First fifty quad-power prime seeds:
      1       2       5       6      69     131     426   1,665   2,129   2,696
  5,250   7,929   9,689  13,545  14,154  14,286  16,434  19,760  25,739  27,809
 29,631  36,821  41,819  46,619  48,321  59,030  60,500  61,955  62,321  73,610
 77,685  79,646  80,535  82,655  85,251  86,996  91,014  96,566  97,739 105,939
108,240 108,681 119,754 122,436 123,164 126,489 140,636 150,480 153,179 163,070

First quad-power prime seed greater than:
   one million is 1,009,286 (the one hundred and forty-first)
   two million is 2,015,496 (the two hundred and thirty-fourth)
 three million is 3,005,316 (the three hundred and nineteenth)
  four million is 4,004,726 (the three hundred and eighty-third)
  five million is 5,023,880 (the four hundred and fifty-second)
   six million is 6,000,554 (the five hundred and fourteenth)
 seven million is 7,047,129 (the five hundred and sixty-seventh)
 eight million is 8,005,710 (the six hundred and first)
  nine million is 9,055,151 (the six hundred and forty-fifth)
   ten million is 10,023,600 (the seven hundred and first)

Raku

<lang perl6>use Lingua::EN::Numbers;

my @qpps = lazy (1..*).hyper(:2000batch).grep: -> \n { my \k = n + 1; (n+k).is-prime && (n²+k).is-prime && (n³+k).is-prime && (n⁴+k).is-prime }

say "First fifty quad-power prime seeds:\n" ~ @qpps[^50].batch(10)».&comma».fmt("%7s").join: "\n";

say "\nFirst quad-power prime seed greater than:";

for 1..5 {

   my $threshold = Int(1e6 * $_);
   my $key = @qpps.first: * > $threshold, :k;
   say "{$threshold.&cardinal.fmt: '%13s'} is the {ordinal-digit $key + 1}: {@qpps[$key].&comma}";

}</lang>

Output:
First fifty quad-power prime seeds:
      1       2       5       6      69     131     426   1,665   2,129   2,696
  5,250   7,929   9,689  13,545  14,154  14,286  16,434  19,760  25,739  27,809
 29,631  36,821  41,819  46,619  48,321  59,030  60,500  61,955  62,321  73,610
 77,685  79,646  80,535  82,655  85,251  86,996  91,014  96,566  97,739 105,939
108,240 108,681 119,754 122,436 123,164 126,489 140,636 150,480 153,179 163,070

First quad-power prime seed greater than:
  one million is the 141st: 1,009,286
  two million is the 234th: 2,015,496
three million is the 319th: 3,005,316
 four million is the 383rd: 4,004,726
 five million is the 452nd: 5,023,880

Wren

Library: Wren-gmp
Library: Wren-fmt

GMP allows us to stretch a little more. <lang ecmascript>import "./gmp" for Mpz import "./fmt" for Fmt

var p = Mpz.new()

var isQuadPowerPrimeSeed = Fn.new { |n|

   p.setUi(n)
   var k = n + 1
   return (p + k).probPrime(15) > 0 &&
          (p.mul(n) + k).probPrime(15) > 0 &&
          (p.mul(n) + k).probPrime(15) > 0 &&
          (p.mul(n) + k).probPrime(15) > 0

}

var qpps = [] var n = 1 while (qpps.count < 50) {

   if (isQuadPowerPrimeSeed.call(n)) qpps.add(n)
   n = n + 1

} System.print("First fifty quad-power prime seeds:") Fmt.tprint("$,7d", qpps, 10)

System.print("\nFirst quad-power prime seed greater than:") var m = 1 var c = 50 while (true) {

   if (isQuadPowerPrimeSeed.call(n)) {
       c = c + 1
       if (n > m * 1e6) {
           Fmt.print(" $2d million is the $r: $,10d", m, c, n)
           m = m + 1
           if (m == 11) return
       }
   }
   n = n + 1

}</lang>

Output:
First fifty quad-power prime seeds:
      1       2       5       6      69     131     426   1,665   2,129   2,696 
  5,250   7,929   9,689  13,545  14,154  14,286  16,434  19,760  25,739  27,809 
 29,631  36,821  41,819  46,619  48,321  59,030  60,500  61,955  62,321  73,610 
 77,685  79,646  80,535  82,655  85,251  86,996  91,014  96,566  97,739 105,939 
108,240 108,681 119,754 122,436 123,164 126,489 140,636 150,480 153,179 163,070 

First quad-power prime seed greater than:
  1 million is the 141st:  1,009,286
  2 million is the 234th:  2,015,496
  3 million is the 319th:  3,005,316
  4 million is the 383rd:  4,004,726
  5 million is the 452nd:  5,023,880
  6 million is the 514th:  6,000,554
  7 million is the 567th:  7,047,129
  8 million is the 601st:  8,005,710
  9 million is the 645th:  9,055,151
 10 million is the 701st: 10,023,600