How to resolve the algorithm Goldbach's comet step by step in the AWK programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Goldbach's comet step by step in the AWK programming language
Table of Contents
Problem Statement
Goldbach's comet is the name given to a plot of the function g(E), the so-called Goldbach function. The Goldbach function is studied in relation to Goldbach's conjecture. The function g(E) is defined for all even integers E>2 to be the number of different ways in which E can be expressed as the sum of two primes.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Goldbach's comet step by step in the AWK programming language
Source code in the awk programming language
# 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)
}
You may also check:How to resolve the algorithm Magic 8-ball step by step in the 11l programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the 11l programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the Oz programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Julia programming language
You may also check:How to resolve the algorithm Superellipse step by step in the jq programming language