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.



integer pn = 1, -- prime numb
        lp = 2, -- last prime
        lg = 0, -- last gap
        pd = 0  -- prev d
sequence cr = {0,0},    -- curr run [a,d]
         mr = {{0},{0}} -- max runs  ""
while true do
    pn += 1
    integer p = get_prime(pn), gap = p-lp,
            d = compare(gap,lg)
    if p>1e6 then exit end if
    if d then
        integer i = (3-d)/2
        cr[i] = iff(d=pd?cr[i]:lp!=2)+1
        if cr[i]>mr[i][1] then mr[i] = {cr[i],pn} end if
    end if
    {pd,lp,lg} = {d,p,gap}
end while

for run=1 to 2 do
    integer {l,e} = mr[run]
    sequence p = apply(tagset(e,e-l),get_prime),
             g = sq_sub(p[2..$],p[1..$-1])
    printf(1,"longest %s run length %d: %v gaps: %v\n",
       {{"ascending","descending"}[run],length(p),p,g})
end for
