Successive prime differences: Difference between revisions

Line 30:
:#https://www.primepuzzles.net/puzzles/puzz_011.htm
:#https://matheplanet.de/matheplanet/nuke/html/viewtopic.php?topic=232720&start=0
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f SUCCESSIVE_PRIME_DIFFERENCES.AWK
BEGIN {
for (i=lo=0; i<=hi=1000000; i++) {
if (is_prime(i)) {
p_arr[++p] = i
}
}
printf("there are %d primes between %d - %d\n",p,lo,hi)
fmt = "%-11s %5s %-15s %s\n"
printf(fmt,"differences","count","first group","last group")
for (a=1; a<=split("2;1;2,2;2,4;4,2;6,4,2;2,4,6;100;112",diff_arr,";"); a++) {
diff_leng = split(diff_arr[a],tmp_arr,",")
first_set = last_set = ""
count = 0
for (b=1; b<=p; b++) {
str = ""
for (c=1; c<=diff_leng; c++) {
if (p_arr[b+c-1] + tmp_arr[c] == p_arr[b+c]) {
str = (str == "") ? (p_arr[b+c-1] "," p_arr[b+c]) : (str "," p_arr[b+c])
}
}
if (gsub(/,/,"&",str) == diff_leng) {
count++
if (first_set == "") {
first_set = str
}
last_set = str
}
}
printf(fmt,diff_arr[a],count,first_set,last_set)
}
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
</lang>
{{out}}
<pre>
there are 78498 primes between 0 - 1000000
differences count first group last group
2 8169 3,5 999959,999961
1 1 2,3 2,3
2,2 1 3,5,7 3,5,7
2,4 1393 5,7,11 999431,999433,999437
4,2 1444 7,11,13 997807,997811,997813
6,4,2 306 31,37,41,43 997141,997147,997151,997153
2,4,6 279 17,19,23,29 997097,997099,997103,997109
100 2 396733,396833 838249,838349
112 1 370261,370373 370261,370373
</pre>
=={{header|C sharp}}==
<lang csharp>using System;
477

edits