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 runs(f, arr) {
 
    var run = 0
    var diff = 0
    var diffs = []
 
    arr.each_cons(2, {|p1,p2|
        var curr_diff = (p2 - p1)
        f(curr_diff, diff) ? ++run : (run = 1)
        diff = curr_diff
        diffs << run
    })
 
    var max  = diffs.max
    var runs = []
 
    diffs.indices_by { _ == max }.each {|i|
        runs << arr.slice(i - max + 1, i + 1)
    }
 
    return runs
}
 
var limit = 1e6
var primes = limit.primes
 
say "Longest run(s) of ascending prime gaps up to #{limit.commify}:"
say runs({|a,b| a > b }, primes).join("\n")
 
say "\nLongest run(s) of descending prime gaps up to #{limit.commify}:"
say runs({|a,b| a < b }, primes).join("\n")