Goldbach's comet

From Rosetta Code
Revision as of 09:59, 7 May 2022 by Drkameleon (talk | contribs) (Added new task + add example solution (in Arturo))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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



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