Goldbach's comet: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Phix}}: minor tidy, added requires("1.0.2"), since it does)
Line 435: Line 435:
--</span>
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4000</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4000</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$],</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$],</span>
<span style="color: #000000;">goldbach</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reinstate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">goldbach</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reinstate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
Line 455: Line 456:
<span style="color: #008080;">include</span> <span style="color: #000000;">IupGraph</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">IupGraph</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">get_data</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">get_data</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*graph*/</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">xR</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">),</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">goldbach</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)),</span><span style="color: #004600;">CD_RED</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">yR</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">goldbach</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xR</span><span style="color: #0000FF;">),</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">goldbach</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)),</span><span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">xB</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">),</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">goldbach</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)),</span><span style="color: #004600;">CD_DARK_GREEN</span><span style="color: #0000FF;">}}</span>
<span style="color: #000000;">yB</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">goldbach</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xB</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">xG</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">yG</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">extract</span><span style="color: #0000FF;">(</span><span style="color: #000000;">goldbach</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xG</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{{</span><span style="color: #7060A8;">sq_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xR</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">yR</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_RED</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">sq_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xB</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">yB</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">sq_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xG</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">yG</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_DARK_GREEN</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">IupGraph</span><span style="color: #0000FF;">(</span><span style="color: #000000;">get_data</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RASTERSIZE=640x440"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">IupGraph</span><span style="color: #0000FF;">(</span><span style="color: #000000;">get_data</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RASTERSIZE=640x440,MARKSTYLE=PLUS"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XTICK=%d,XMIN=0,XMAX=%d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">/</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XTICK=%d,XMIN=0,XMAX=%d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">/</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YTICK=20,YMIN=0,YMAX=%d"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">goldbach</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">20</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YTICK=20,YMIN=0,YMAX=%d"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">goldbach</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">20</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MODE=MARK, MARKSTYLE=PLUS"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Goldbach's comet",MINSIZE=400x300`</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Goldbach's comet"`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MINSIZE=400x300"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
<!--</lang>-->

Revision as of 13:06, 10 May 2022

Task
Goldbach's comet
You are encouraged to solve this task according to the task description, using any language you may know.
This page uses content from Wikipedia. The original article was at Goldbach's comet. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)

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.

Examples
  • G(4) = 1, since 4 can only be expressed as the sum of one distinct pair of primes (4 = 2 + 2)
  • G(22) = 3, since 22 can be expressed as the sum of 3 distinct pairs of primes (22 = 11 + 11 = 5 + 17 = 3 + 19)


Task
  • Find and show (preferably, in a neat 10x10 table) the first 100 G numbers (that is: the result of the G function described above, for the first 100 even numbers >= 4)
  • Find and display the value of G(1000000)


Stretch
  • Calculate the values of G up to 2000 (inclusive) and display the results in a scatter 2d-chart, aka the Goldbach's Comet


See also



ALGOL 68

Generates an ASCII-Art scatter plot - the vertical axis is n/10 and the hotizontal is G(n).

<lang algol68>BEGIN # calculate values of the Goldbach function G where G(n) is the number #

     # of prime pairs that sum to n, n even and > 2                         #
     # generates an ASCII scatter plot of G(n) up to G(2000)                #
     # (Goldbach's Comet)                                                   #
   PR read "primes.incl.a68" PR          # include prime utilities          #
   INT max prime = 1 000 000;            # maximum number we will consider  #
   INT max plot  =     2 000;            # maximum G value for the comet    #
   []BOOL prime = PRIMESIEVE max prime;  # sieve of primes to max prime     #
   [ 0 : max plot ]INT g2;               # table of G values: g2[n] = G(2n) #
   # construct the table of G values #
   FOR n FROM LWB g2 TO UPB g2 DO g2[ n ] := 0 OD;
   g2[ 4 ] := 1;                     # 4 is the only sum of two even primes #
   FOR p FROM 3 BY 2 TO max plot OVER 2 DO
       IF prime[ p ] THEN
           g2[ p + p ] +:= 1;
           FOR q FROM p + 2 BY 2 TO max plot - p DO
               IF prime[ q ] THEN
                   g2[ p + q ] +:= 1
               FI
           OD
       FI
   OD;
   # show the first hundred G values #
   INT c := 0;
   FOR n FROM 4 BY 2 TO 202 DO
       print( ( whole( g2[ n ], -4 ) ) );
       IF ( c +:= 1 ) = 10 THEN print( ( newline ) ); c := 0 FI
   OD;
   # show G( 1 000 000 ) #
   INT gm := 0;
   FOR p FROM 3 TO max prime OVER 2 DO
       IF prime[ p ] THEN
           IF prime[ max prime - p ] THEN
               gm +:= 1
           FI
       FI
   OD;
   print( ( "G(", whole( max prime, 0 ), "): ", whole( gm, 0 ), newline ) );
   # find the maximum value of G up to the maximum plot size #
   INT max g := 0;
   FOR n FROM 2 BY 2 TO max plot DO
       IF g2[ n ] > max g THEN max g := g2[ n ] FI
   OD;
   # draw an ASCII scatter plot of G, each position represents 5 G values #
   # the vertical axis is n/10, the horizontal axis is G(n) #
   INT plot step = 10;
   STRING plot value = " .-+=*%$&#@";
   FOR g FROM 0 BY plot step TO max plot - plot step DO
       [ 0 : max g ]INT values;
       FOR v pos FROM LWB values TO UPB values DO values[ v pos ] := 0 OD;
       INT max v := 0;
       FOR g element FROM g BY 2 TO g + ( plot step - 1 ) DO
           INT g2 value = g2[ g element ];
           values[ g2 value ] +:= 1;
           IF g2 value > max v THEN max v := g2 value FI
       OD;
       print( ( IF g MOD 100 = 90 THEN "+" ELSE "|" FI ) );
       FOR v pos FROM 1 TO max v DO # exclude 0 values from the plot #
           print( ( plot value[ values[ v pos ] + 1 ] ) )
       OD;
       print( ( newline ) )
   OD

END</lang>

Output:
   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

|+ |.= | -+ | -.- | --. | --. | .. .- | +.. | -- . + ... . . | .- - | +. . | ... . . | ..... | .... . | .. . .. | .. . . . | .- . . | .. . .. + ... . . | -.. . | ... . . | - . .. | . + . | . .. . . | .- . . | .-. . | - . . . | . . . .. + - .. . | ... . . | .. . . . | . .- . | . . . . . | .. . .. | .- . . | - . . . | .. . . . | .. .. . + . - . . | . .. . . | .- . . | . .. . . | . .. . . | .-. . | . - . . | .. . . . | . ... . | . . . . . + - . .. | ..- . | - . . . | . . . .. | .- . . | .. . - | - . - | -. . . | .. . . . | .. . . . + . -. . | ... . . | . .. - | . ... . | . .. . . | ... . . | . . . . . | . . . . . | . . . . . | + . . + . . . . . | .. - . | .... . | .- . . | . . . . . | -- . | .. . . . | - . .. | .. . . . | .. . . . + . . . . . | . - . . | .. . . . | . .. . . | .. - . | . .. . . | .. . . . | -. . . | . .. . . | . . . . . + -. . . | - . . . | . . - . | - . . . | .. . . . | . . . .. | . .. . . | ... - | . . . . . | . . . . . + . . . . . | - . . . | . . . . . | - . . . | . . . . . | - . . . | .. . . . | - . . . | .. . . . | . .. . . + . . . . . | .- . . | . .. . . | . . . . . | . .. . . | . . . . . | . . . . . | . .. . . | .. . . . | - . . . + .- . . | .. . . . | .. . . . | - . . . | . . . . . | . . . .. | .. . . . | .. . . . | ... . . | . .. . . + . . . . . | - . . . | . .. . . | .. . . . | - . . . | . . - . | .. . . . | - . . . | . -. . | - . . . + - . .. | . . . . . | . . . . . | .. . . . | .. . . . | . .. . . | . . . .. | .. . . . | . . . . . | . . . - + .. .. . | . - . . | . . . . . | -. . . | . . . . . | . . - . | .. . . . | . .. . . | . . . . . | . . . . . + .. . . . | . . . . . | . . . . . | . . . . . | - . . . | ... . . | .. . . . | . . . .. | . . . . . | . . . . . + . . . . . | - . . . | . .. . . | . . . . . | . . .. . | - . . . | . . . . . | . .. . . | .. . . . | . . . . . + . . - . | . - . . | .. . - | . . . . . | . . . . . | . . . . . | .. . . . | . . . . . | . . . . . | . . . . . + .. . . . | . - . . | . . .. . | - . . . | . . . . . | .. . . . | . . . . . | . . . . . | -. . . | - . . . + .. . . .

Arturo

<lang rebol>G: function [n][

   size select 2..n/2 'x ->
       and? [prime? x][prime? n-x]

]

print "The first 100 G values:" loop split.every: 10 map select 4..202 => even? => G 'row [

   print map to [:string] row 'item -> pad item 3

]

print ["\nG(1000000) =" G 1000000]

csv: join.with:",\n" map select 4..2000 => even? 'x ->

   ~"|x|, |G x|"
write the CSV data to a file which we can then visualize
via our preferred spreadsheet app

write "comet.csv" csv</lang>

Output:
The first 100 G values:
  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

Here, you can find the result of the visualization - or the "Goldbach's comet": 2D-chart of all G values up to 2000

J

Task implementation:

<lang J> 10 10$#/.~4,/:~ 0-.~,(<:/~ * +/~) p:1+i.p:inv 202

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</lang>

And, for G(1e6): <lang j>

  -:+/1 p: 1e6-p:i.p:inv 1e6

5402</lang>

Explanation:

For the first part, the easiest approach seems to be to brute force it. The first 100 numbers starting with 4 end at 202, so we start by finding all primes less than 202 (and sum all pairs of these primes).

Also, we can eliminate an odd/even test on these sums of primes by excluding 2 from our list of primes and including 4 as an explicit constant.

Also, since we are only concerned about distinct sums, we eliminate all pairs where the first prime in the sum exceeds the second prime in the sum.

So.. we compute a couple thousand sums, sort them in numeric order (prepending that list with 4), count how many times each unique value appears, and order the first 100 of those frequencies in a 10x10 table.

---

For G(1e6) that brute force approach becomes inefficient -- instead of about 2000 sums, almost 600 of which are relevant, we would need to find over 6e9 sums, and about 6e9 of them would be irrelevant.

Instead, for G(1e6), we find all primes less than a million, subtract each from 1 million and count how many of the differences are prime and cut that in half. We cut that sum in half because this approach counts each pair twice (once with the smallest value first, again with the smallest value second -- since 1e6 is not the square of a prime we do not have a prime which appears twice in one of these sums).

Julia

Run in VS Code or REPL to view and save the plot. <lang ruby>using Combinatorics using Plots using Primes

g(n) = iseven(n) ? count(p -> all(isprime, p), partitions(n, 2)) : error("n must be even")

println("The first 100 G numbers are: ")

foreach(p -> print(lpad(p[2], 4), p[1] % 10 == 0 ? "\n" : ""), map(g, 4:2:202) |> enumerate)

println("\nThe value of G(1000000) is ", g(1_000_000))

x = collect(2:2002) y = map(g, 2x) scatter(x, y, markerstrokewidth = 0, color = ["red", "blue", "green"][mod1.(x, 3)])

</lang>

Output:

The first 100 G numbers are:

  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

The value of G(1000000) is 5402

Phix

Library: Phix/pGUI
Library: Phix/online

You can run this online here.

--
-- demo\rosetta\Goldbachs_comet.exw
-- ================================
--
-- Note: this plots n/2 vs G(n) for n=6 to 4000 by 2, matching wp and
--       Algol 68, Python, and Raku. However, while not wrong, Arturo 
--       and Wren apparently plot n vs G(n) for n=6 to 2000 by 2, so
--       should you spot any (very) minor differences, that'd be why.
--
with javascript_semantics
requires("1.0.2")

constant limit = 4000
sequence primes = get_primes_le(limit)[2..$],
       goldbach = reinstate(repeat(0,limit),{4},{1})
for i=1 to length(primes) do
    for j=i to length(primes) do
        integer s = primes[i] + primes[j]
        if s>limit then exit end if
        goldbach[s] += 1
    end for
end for
 
sequence fhg = extract(goldbach,tagstart(4,100,2))
string fhgs = join_by(fhg,1,10," ","\n","%2d")
integer gm = sum(apply(sq_sub(1e6,get_primes_le(499999)[2..$]),is_prime))
printf(1,"The first 100 G values:\n%s\n\nG(1,000,000) = %,d\n",{fhgs,gm})

include pGUI.e
include IupGraph.e
 
function get_data(Ihandle /*graph*/)
    return {{tagset(limit/2,3,3),extract(goldbach,tagset(limit,6,6)),CD_RED},
            {tagset(limit/2,4,3),extract(goldbach,tagset(limit,8,6)),CD_BLUE},
            {tagset(limit/2,5,3),extract(goldbach,tagset(limit,10,6)),CD_DARK_GREEN}}
end function

IupOpen()
Ihandle graph = IupGraph(get_data,"RASTERSIZE=640x440,MARKSTYLE=PLUS")
IupSetAttributes(graph,"XTICK=%d,XMIN=0,XMAX=%d",{limit/20,limit/2})
IupSetAttributes(graph,"YTICK=20,YMIN=0,YMAX=%d",{max(goldbach)+20})
Ihandle dlg = IupDialog(graph,`TITLE="Goldbach's comet",MINSIZE=400x300`)
IupShow(dlg)
if platform()!=JS then 
    IupMainLoop()
    IupClose()
end if
Output:
The first 100 G values:
 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(1,000,000) = 5,402

Python

<lang python>from matplotlib.pyplot import scatter, show from sympy import isprime

def g(n):

   assert n > 2 and n % 2 == 0, 'n in goldbach function g(n) must be even'          
   count = 0
   for i in range(1, n//2 + 1):
       if isprime(i) and isprime(n - i):
           count += 1
   return count

print('The first 100 G numbers are:')

col = 1 for n in range(4, 204, 2):

   print(str(g(n)).ljust(4), end = '\n' if (col % 10 == 0) else )
   col += 1

print('\nThe value of G(1000000) is', g(1_000_000))

x = range(4, 4002, 2) y = [g(i) for i in x] colors = [["red", "blue", "green"][(i // 2) % 3] for i in x] scatter([i // 2 for i in x], y, marker='.', color = colors)

show()</lang>

Output:
The first 100 G numbers are:
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   

The value of G(1000000) is 5402

Raku

For the stretch, actually generates a plot, doesn't just calculate values to be plotted by a third party application. Deviates slightly from the stretch goal, plots the first two thousand defined values rather than the values up to two thousand that happen to be defined. (More closely matches the wikipedia example image.)

<lang perl6>sub G (Int $n) { +(2..$n/2).grep: { .is-prime && ($n - $_).is-prime } }

  1. Task

put "The first 100 G values:\n", (^100).map({ G 2 × $_ + 4 }).batch(10)».fmt("%2d").join: "\n";

put "\nG 1_000_000 = ", G 1_000_000;

  1. Stretch

use SVG; use SVG::Plot;

my @x = map 2 × * + 4, ^2000; my @y = @x.map: &G;

'Goldbachs-Comet-Raku.svg'.IO.spurt: SVG.serialize: SVG::Plot.new(

   width       => 1000,
   height      => 500,
   background  => 'white',
   title       => "Goldbach's Comet",
   x           => @x,
   values      => [@y,],

).plot: :points; </lang>

Output:
The first 100 G values:
 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 1_000_000 = 5402

Stretch goal: (offsite SVG image) Goldbachs-Comet-Raku.svg

Wren

Library: Wren-math
Library: Wren-trait
Library: Wren-fmt

<lang ecmascript>import "./math" for Int import "./trait" for Stepped import "./fmt" for Fmt import "io" for File

var limit = 2000 var primes = Int.primeSieve(limit-1).skip(1).toList var goldbach = {4: 1} for (i in Stepped.new(6..limit, 2)) goldbach[i] = 0 for (i in 0...primes.count) {

   for (j in i...primes.count) {
       var s = primes[i] + primes[j]
       if (s > limit) break
       goldbach[s] = goldbach[s] + 1
   }

}

System.print("The first 100 G values:") var count = 0 for (i in Stepped.new(4..202, 2)) {

   count = count + 1
   Fmt.write("$2d ", goldbach[i])
   if (count % 10 == 0) System.print()

}

primes = Int.primeSieve(499999).skip(1) var gm = 0 for (p in primes) {

   if (Int.isPrime(1e6 - p)) gm = gm + 1

} System.print("\nG(1000000) = %(gm)")

// create .csv file for values up to 2000 for display by an external plotter // the third field being the color (red = 0, blue = 1, green = 2) File.create("goldbachs_comet.csv") { |file|

   for(i in Stepped.new(4..limit, 2)) {
       file.writeBytes("%(i), %(goldbach[i]), %(i/2 % 3)\n")
   }

}</lang>

Output:
The first 100 G values:
 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

XPL0

<lang XPL0> func IsPrime(N); \Return 'true' if N is prime int N, I; [if N <= 2 then return N = 2; if (N&1) = 0 then \even >2\ return false; for I:= 3 to sqrt(N) do

   [if rem(N/I) = 0 then return false;
   I:= I+1;
   ];

return true; ];

int PT(1_000_000);

func G(E); \Ways E can be expressed as sum of two primes int E, C, I, J, T; [C:= 0; I:= 0; loop [J:= I;

       if PT(J) + PT(I) > E then return C;
       loop    [T:= PT(J) + PT(I);
               if T = E then C:= C+1;
               if T > E then quit;
               J:= J+1;
               ];
       I:= I+1;
       ];

];

int I, N; [I:= 0; \make prime table for N:= 2 to 1_000_000 do

       if IsPrime(N) then
               [PT(I):= N;  I:= I+1];

I:= 4; \show first 100 G numbers Format(4, 0); for N:= 1 to 100 do

       [RlOut(0, float(G(I)));
       if rem(N/10) = 0 then CrLf(0);
       I:= I+2;
       ];

CrLf(0); Text(0, "G(1,000,000) = "); IntOut(0, G(1_000_000)); CrLf(0); ]</lang>

Output:
   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(1,000,000) = 5402