Strange unique prime triplets: Difference between revisions

Content added Content deleted
m (Bug fixes)
Line 108: Line 108:
Found 42 strange unique prime triplets up to 30
Found 42 strange unique prime triplets up to 30
Found 241580 strange unique prime triplets up to 1000
Found 241580 strange unique prime triplets up to 1000
</pre>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f STRANGE_UNIQUE_PRIME_TRIPLETS.AWK
# converted from Go
BEGIN {
main(29,1)
main(999,0)
exit(0)
}
function main(n,show, count,i,j,k,s) {
for (i=3; i<=n-4; i+=2) {
if (is_prime(i)) {
for (j=i+2; j<=n-2; j+=2) {
if (is_prime(j)) {
for (k=j+2; k<=n; k+=2) {
if (is_prime(k)) {
s = i + j + k
if (is_prime(s)) {
count++
if (show == 1) {
printf("%2d + %2d + %2d = %d\n",i,j,k,s)
}
}
}
}
}
}
}
}
printf("Unique prime triples 2-%d which sum to a prime: %'d\n\n",n,count)
}
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>
3 + 5 + 11 = 19
3 + 5 + 23 = 31
3 + 5 + 29 = 37
3 + 7 + 13 = 23
3 + 7 + 19 = 29
3 + 11 + 17 = 31
3 + 11 + 23 = 37
3 + 11 + 29 = 43
3 + 17 + 23 = 43
5 + 7 + 11 = 23
5 + 7 + 17 = 29
5 + 7 + 19 = 31
5 + 7 + 29 = 41
5 + 11 + 13 = 29
5 + 13 + 19 = 37
5 + 13 + 23 = 41
5 + 13 + 29 = 47
5 + 17 + 19 = 41
5 + 19 + 23 = 47
5 + 19 + 29 = 53
7 + 11 + 13 = 31
7 + 11 + 19 = 37
7 + 11 + 23 = 41
7 + 11 + 29 = 47
7 + 13 + 17 = 37
7 + 13 + 23 = 43
7 + 17 + 19 = 43
7 + 17 + 23 = 47
7 + 17 + 29 = 53
7 + 23 + 29 = 59
11 + 13 + 17 = 41
11 + 13 + 19 = 43
11 + 13 + 23 = 47
11 + 13 + 29 = 53
11 + 17 + 19 = 47
11 + 19 + 23 = 53
11 + 19 + 29 = 59
13 + 17 + 23 = 53
13 + 17 + 29 = 59
13 + 19 + 29 = 61
17 + 19 + 23 = 59
19 + 23 + 29 = 71
Unique prime triples 2-29 which sum to a prime: 42

Unique prime triples 2-999 which sum to a prime: 241,580
</pre>
</pre>