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.



func IsPrime(N);        \Return 'true' if N > 2 is a prime number
int  N, I;
[if (N&1) = 0 \even number\ then return false;
for I:= 3 to sqrt(N) do
    [if rem(N/I) = 0 then return false;
    I:= I+1;
    ];
return true;
];
 
proc ShowSeq(Dir, Str); \Show longest sequence of distances between primes
int  Dir, Str;
int  Count, MaxCount, N, P, P0, D, D0, I, AP(1000), MaxAP(1000);
[Count:= 0;  MaxCount:= 0;
P0:= 2;  D0:= 0;                \preceding prime and distance
AP(Count):= P0;  Count:= Count+1;
for N:= 3 to 1_000_000-1 do
    if IsPrime(N) then
        [P:= N;                 \got a prime number
        D:= P - P0;             \distance from preceding prime
        if D*Dir > D0*Dir then
            [AP(Count):= P;  Count:= Count+1;
            if Count > MaxCount then        \save best sequence
                [MaxCount:= Count;
                for I:= 0 to MaxCount-1 do
                    MaxAP(I):= AP(I);
                ];
            ]
        else
            [Count:= 0;     \restart sequence
            AP(Count):= P0;  Count:= Count+1; \possible beginning
            AP(Count):= P;   Count:= Count+1;
            ];
        P0:= P;  D0:= D;
        ];
Text(0, "Longest sequence of ");  Text(0, Str);
Text(0, " distances between primes: ");  IntOut(0, MaxCount);  CrLf(0);
for I:= 0 to MaxCount-2 do
    [IntOut(0, MaxAP(I));
    Text(0, " (");
    IntOut(0, MaxAP(I+1) - MaxAP(I));
    Text(0, ") ");
    ];
IntOut(0, MaxAP(I));  CrLf(0);
];
 
[ShowSeq(+1,  "ascending");     \main
 ShowSeq(-1, "descending");
]