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.



function findcps(primelist, fcmp)
  local currlist = {primelist[1]}
  local longlist, prevdiff = currlist, 0
  for i = 2, #primelist do
    local diff = primelist[i] - primelist[i-1]
    if fcmp(diff, prevdiff) then
      currlist[#currlist+1] = primelist[i]
      if #currlist > #longlist then
        longlist = currlist
      end
    else
      currlist = {primelist[i-1], primelist[i]}
    end
    prevdiff = diff
  end
  return longlist
end
 
primegen:generate(nil, 1000000)
cplist = findcps(primegen.primelist, function(a,b) return a>b end)
print("ASC  ("..#cplist.."):  ["..table.concat(cplist, " ").."]")
cplist = findcps(primegen.primelist, function(a,b) return a<b end)
print("DESC ("..#cplist.."):  ["..table.concat(cplist, " ").."]")