Goldbach's comet: Difference between revisions

added AWK
m (→‎{{header|Perl}}: ...and link to image)
(added AWK)
Line 349:
 
Here, you can find the result of the visualization - or the "Goldbach's comet": [https://i.ibb.co/JvLDRc0/Screenshot-2022-05-07-at-11-35-23.png 2D-chart of all G values up to 2000]
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f GOLDBACHS_COMET.AWK
BEGIN {
print("The first 100 G numbers:")
for (n=4; n<=202; n+=2) {
printf("%4d%1s",g(n),++count%10?"":"\n")
}
n = 1000000
printf("\nG(%d): %d\n",n,g(n))
n = 4
printf("G(%d): %d\n",n,g(n))
n = 22
printf("G(%d): %d\n",n,g(n))
exit(0)
}
function g(n, count,i) {
if (n % 2 == 0) { # n must be even
for (i=2; i<=(1/2)*n; i++) {
if (is_prime(i) && is_prime(n-i)) {
count++
}
}
}
return(count)
}
function is_prime(n, d) {
d = 5
if (n < 2) { return(0) }
if (n % 2 == 0) { return(n == 2) }
if (n % 3 == 0) { return(n == 3) }
while (d*d <= n) {
if (n % d == 0) { return(0) }
d += 2
if (n % d == 0) { return(0) }
d += 4
}
return(1)
}
</lang>
{{out}}
<pre>
The first 100 G numbers:
1 1 1 2 1 2 2 2 2 3
3 3 2 3 2 4 4 2 3 4
3 4 5 4 3 5 3 4 6 3
5 6 2 5 6 5 5 7 4 5
8 5 4 9 4 5 7 3 6 8
5 6 8 6 7 10 6 6 12 4
5 10 3 7 9 6 5 8 7 8
11 6 5 12 4 8 11 5 8 10
5 6 13 9 6 11 7 7 14 6
8 13 5 8 11 7 9 13 8 9
 
G(1000000): 5402
G(4): 1
G(22): 3
</pre>
 
=={{header|FreeBASIC}}==
477

edits