Consecutive primes with ascending or descending differences

From Rosetta Code
Revision as of 19:45, 2 April 2021 by Tigerofdarkness (talk | contribs) (Created new prime related task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Consecutive primes with ascending or descending differences 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.
Task



Find and display here on this page, the longest sequence of consecutive prime numbers where the differences between the primes are strictly ascending.

Do the same for sequences of primes where the differences are strictly descending.

In both cases, show the sequence for primes   <   1 000 000.

If there are multiple sequences of the same length, only the first need be shown.



ALGOL 68

<lang algol68>BEGIN # find sequences of primes where the gaps between the elements #

     # are strictly ascending/descending                            #
   # reurns a list of primes up to n #
   PROC prime list = ( INT n )[]INT:
        BEGIN
           # sieve the primes to n #
           INT no = 0, yes = 1;
           [ 1 : n ]INT p;
           p[ 1 ] := no; p[ 2 ] := yes;
           FOR i FROM 3 BY 2 TO n DO p[ i ] := yes OD;
           FOR i FROM 4 BY 2 TO n DO p[ i ] := no  OD;
           FOR i FROM 3 BY 2 TO ENTIER sqrt( n ) DO
               IF p[ i ] = yes THEN FOR s FROM i * i BY i + i TO n DO p[ s ] := no OD FI
           OD;
           # replace the sieve with a list #
           INT p pos := 0;
           FOR i TO n DO IF p[ i ] = yes THEN p[ p pos +:= 1 ] := i FI OD;
           p[ 1 : p pos ]
        END # prime list # ;
   # find the longest sequence of primes where the successive differences are ascending #
   INT max number   = 1 000 000;
   []INT primes     = prime list( max number );
   INT asc length  := 0;
   INT asc start   := 0;
   INT desc length := 0;
   INT desc start  := 0;
   FOR p FROM LWB primes TO UPB primes DO
       INT prev diff := 0;
       INT length    := 1;
       FOR s FROM p + 1 TO UPB primes
       WHILE INT diff = primes[ s ] - primes[ s - 1 ];
             diff > prev diff
       DO
           length   +:= 1;
           prev diff := diff
       OD;
       IF length > asc length THEN
           # found a longer sequence #
           asc length := length;
           asc start  := p
       FI
   OD;
   # find the longest sequence of primes where the successive differences are descending #
   FOR p FROM LWB primes TO UPB primes DO
       INT prev diff := max number + 1;
       INT length    := 1;
       FOR s FROM p + 1 TO UPB primes
       WHILE INT diff = primes[ s ] - primes[ s - 1 ];
             diff < prev diff
       DO
           length   +:= 1;
           prev diff := diff
       OD;
       IF length > desc length THEN
           # found a longer sequence #
           desc length := length;
           desc start  := p
       FI
   OD;
   # show the sequences #
   print( ( "For primes up to ", whole( max number, 0 ), newline ) );
   print( ( "    Longest sequence of primes with ascending differences contains "
          , whole( asc length, 0 )
          , " primes, first such sequence:"
          , newline
          , "(differences in brackets):"
          , newline
          , "        "
          )
        );
   print( ( whole( primes[ asc start ], 0 ) ) );
   FOR p FROM asc start + 1 TO asc start + ( asc length - 1 ) DO
       print( ( " (", whole( primes[ p ] - primes[ p - 1 ], 0 ), ") ", whole( primes[ p ], 0 ) ) )
   OD;
   print( ( newline ) );
   print( ( "    Longest sequence of primes with descending differences contains "
          , whole( asc length, 0 )
          , " primes, first such sequence:"
          , newline
          , "(differences in brackets):"
          , newline
          , "        "
          )
        );
   print( ( whole( primes[ desc start ], 0 ) ) );
   FOR p FROM desc start + 1 TO desc start + ( desc length - 1 ) DO
       print( ( " (", whole( primes[ p ] - primes[ p - 1 ], 0 ), ") ", whole( primes[ p ], 0 ) ) )
   OD;
   print( ( newline ) )

END</lang>

Output:
For primes up to 1000000
    Longest sequence of primes with ascending differences contains 8 primes, first such sequence:
(differences in brackets):
        128981 (2) 128983 (4) 128987 (6) 128993 (8) 129001 (10) 129011 (12) 129023 (14) 129037
    Longest sequence of primes with descending differences contains 8 primes, first such sequence:
(differences in brackets):
        322171 (22) 322193 (20) 322213 (16) 322229 (8) 322237 (6) 322243 (4) 322247 (2) 322249