Example 2: Specifying more than one difference between s'primes leads to groups of size one greater than the number of differences. Differences of 2, 4 leads to the groups:

(5, 7, 11), (11, 13, 17), (17, 19, 23), (41, 43, 47), ....

In the first group 7 is two more than 5 and 11 is four more than 7; as well as 5, 7, and 11 being successive primes.
Differences are checked in the order of the values given, (differences of 4, 2 would give different groups entirely).

Task

In each case use a list of primes less than 1_000_000

For the following Differences show the first and last group, as well as the number of groups found:

Differences of 2.

Differences of 1.

Differences of 2, 2.

Differences of 2, 4.

Differences of 4, 2.

Differences of 6, 4, 2.

Show output here.

Note: Generation of a list of primes is a secondary aspect of the task. Use of a built in function, well known library, or importing/use of prime generators from other Rosetta Code tasks is encouraged.

For primes less than 1,000,000:-
For differences of [2] ->
First group = [3 5]
Last group = [999959 999961]
Number found = 8169
For differences of [1] ->
First group = [2 3]
Last group = [2 3]
Number found = 1
For differences of [2 2] ->
First group = [3 5 7]
Last group = [3 5 7]
Number found = 1
For differences of [2 4] ->
First group = [5 7 11]
Last group = [999431 999433 999437]
Number found = 1393
For differences of [4 2] ->
First group = [7 11 13]
Last group = [997807 997811 997813]
Number found = 1444
For differences of [6 4 2] ->
First group = [31 37 41 43]
Last group = [997141 997147 997151 997153]
Number found = 306

procedure test(sequence differences) sequence res = {} integer ld = length(differences) for i=1 to length(primes)-ld do integer pi = primes[i] for j=1 to ld do pi += differences[j] if pi!=primes[i+j] then pi = 0 exit end if end for if pi!=0 then res = append(res,primes[i..i+ld]) end if end for res = {differences,length(res),res[1],res[$]} printf(1,"%8v : %8d %14v...%v\n",res)end procedure

while primes[$]<1_000_000 do add_block() end whileprimes = primes[1..abs(binary_search(1_000_000,primes))-1]constant differences = {{2},{1},{2,2},{2,4},{4,2},{6,4,2}}printf(1,"Differences Count First Last\n")for i=1 to length(differences) do test(differences[i]) end for

say ("...for differences #{diffs}, there are #{groups.len} groups, where ","the first group = #{groups.first} and the last group = #{groups.last}")}

Output:

Groups of successive primes <= 1,000,000:
...for differences [2], there are 8169 groups, where the first group = [3, 5] and the last group = [999959, 999961]
...for differences [1], there are 1 groups, where the first group = [2, 3] and the last group = [2, 3]
...for differences [2, 2], there are 1 groups, where the first group = [3, 5, 7] and the last group = [3, 5, 7]
...for differences [2, 4], there are 1393 groups, where the first group = [5, 7, 11] and the last group = [999431, 999433, 999437]
...for differences [4, 2], there are 1444 groups, where the first group = [7, 11, 13] and the last group = [997807, 997811, 997813]
...for differences [6, 4, 2], there are 306 groups, where the first group = [31, 37, 41, 43] and the last group = [997141, 997147, 997151, 997153]