Goldbach's comet: Difference between revisions

m
syntax highlighting fixup automation
(Added AutoHotkey)
m (syntax highlighting fixup automation)
Line 32:
Generates an ASCII-Art scatter plot - the vertical axis is n/10 and the hotizontal is G(n).
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight 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) #
Line 94:
print( ( newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 313:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">G: function [n][
size select 2..n/2 'x ->
and? [prime? x][prime? n-x]
Line 330:
; write the CSV data to a file which we can then visualize
; via our preferred spreadsheet app
write "comet.csv" csv</langsyntaxhighlight>
 
{{out}}
Line 351:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">c := 0
while (c<100)
if (x := g(A_Index))
Line 372:
Return false
Return true
}</langsyntaxhighlight>
{{out}}
<pre>1 1 1 2 1 2 2 2 2 3
Line 386:
g(1000000) : 5402</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f GOLDBACHS_COMET.AWK
BEGIN {
Line 424:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 445:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Uinteger) As Boolean
If ValorEval <= 1 Then Return False
For i As Integer = 2 To Int(Sqr(ValorEval))
Line 473:
 
Print !"\nThe value of G(1000000) is "; g(1000000)
Sleep</langsyntaxhighlight>
{{out}}
<pre>The first 100 G numbers are:
Line 494:
Task implementation:
 
<langsyntaxhighlight Jlang="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
Line 504:
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</langsyntaxhighlight>
 
And, for G(1e6):
<syntaxhighlight lang="j">
<lang j>
-:+/1 p: 1e6-p:i.p:inv 1e6
5402</langsyntaxhighlight>
 
Explanation:
Line 529:
=={{header|Julia}}==
Run in VS Code or REPL to view and save the plot.
<langsyntaxhighlight lang="ruby">using Combinatorics
using Plots
using Primes
Line 544:
y = map(g, 2x)
scatter(x, y, markerstrokewidth = 0, color = ["red", "blue", "green"][mod1.(x, 3)])
</langsyntaxhighlight>{{out}}<pre>
The first 100 G numbers are:
1 1 1 2 1 2 2 2 2 3
Line 561:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function T(t) return setmetatable(t, {__index=table}) end
table.range = function(t,n) local s=T{} for i=1,n do s[i]=i end return s end
table.map = function(t,f) local s=T{} for i=1,#t do s[i]=f(t[i]) end return s end
Line 589:
g = T{}:range(100):map(function(n) return goldbach(2+n*2) end)
g:map(function(n) return string.format("%2d ",n) end):batch(10,function(t) print(t:concat()) end)
print("G(1000000) = "..goldbach(1000000))</langsyntaxhighlight>
{{out}}
<pre>The first 100 G numbers:
Line 605:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[GoldbachFuncion]
GoldbachFuncion[e_Integer] := Module[{ps},
ps = Prime[Range[PrimePi[e/2]]];
Line 612:
Grid[Partition[GoldbachFuncion /@ Range[4, 220, 2], 10]]
GoldbachFuncion[10^6]
DiscretePlot[GoldbachFuncion[e], {e, 4, 2000}, Filling -> None]</langsyntaxhighlight>
{{out}}
<pre>1 1 1 2 1 2 2 2 2 3
Line 632:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 667:
binmode $fh;
print $fh $gd->png();
close $fh;</langsyntaxhighlight>
{{out}}
<pre> 1 1 1 2 1 2 2 2 2 3
Line 688:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/goldbach.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Goldbachs_comet.exw
Line 736:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 756:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">main =>
println("First 100 G numbers:"),
foreach({G,I} in zip(take([G: T in 1..300, G=g(T),G>0],100),1..100))
Line 767:
{1 : I in 1..N // 2,
prime(I),prime(N-I)}.len,
0).</langsyntaxhighlight>
 
{{out}}
Line 785:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from matplotlib.pyplot import scatter, show
from sympy import isprime
 
Line 809:
colors = [["red", "blue", "green"][(i // 2) % 3] for i in x]
scatter([i // 2 for i in x], y, marker='.', color = colors)
show()</langsyntaxhighlight>{{out}}
<pre>
The first 100 G numbers are:
Line 829:
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 [[wp:Goldbach's_comet|wikipedia example image]].)
 
<syntaxhighlight lang="raku" perl6line>sub G (Int $n) { +(2..$n/2).grep: { .is-prime && ($n - $_).is-prime } }
 
# Task
Line 851:
values => [@y,],
).plot: :points;
</syntaxhighlight>
</lang>
{{out}}
<pre>The first 100 G values:
Line 870:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// primal = "0.3"
// plotters = "0.3.2"
Line 944:
Err(error) => eprintln!("Error: {}", error),
}
}</langsyntaxhighlight>
 
{{out}}
Line 967:
{{trans|FreeBASIC}}
For performance reasons only '''g(100000)''' is calculated. The value "810" has been verified and is correct.
<syntaxhighlight lang="text">Print "The first 100 G numbers are:"
c = 1
 
Line 1,024:
Loop
 
Return (c@)</langsyntaxhighlight>
{{out}}
<pre>The first 100 G numbers are:
Line 1,049:
{{libheader|Wren-plot}}
This follows the Raku example in plotting the first two thousand G values rather than the values up to G(2000) in order to produce an image something like the image in the Wikipedia article.
<langsyntaxhighlight lang="ecmascript">import "dome" for Window
import "graphics" for Canvas, Color
import "./math2" for Int
Line 1,126:
}
 
var Game = Main.new()</langsyntaxhighlight>
 
{{out}}
Line 1,146:
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">
<lang XPL0>
func IsPrime(N); \Return 'true' if N is prime
int N, I;
Line 1,189:
Text(0, "G(1,000,000) = "); IntOut(0, G(1_000_000));
CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
10,333

edits