Goldbach's comet: Difference between revisions
m
syntax highlighting fixup automation
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) 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}}
<
# 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</
{{out}}
<pre>
Line 313:
=={{header|Arturo}}==
<
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</
{{out}}
Line 351:
=={{header|AutoHotkey}}==
<
while (c<100)
if (x := g(A_Index))
Line 372:
Return false
Return true
}</
{{out}}
<pre>1 1 1 2 1 2 2 2 2 3
Line 386:
g(1000000) : 5402</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f GOLDBACHS_COMET.AWK
BEGIN {
Line 424:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 445:
=={{header|FreeBASIC}}==
<
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</
{{out}}
<pre>The first 100 G numbers are:
Line 494:
Task implementation:
<
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</
And, for G(1e6):
<syntaxhighlight lang="j">
-:+/1 p: 1e6-p:i.p:inv 1e6
5402</
Explanation:
Line 529:
=={{header|Julia}}==
Run in VS Code or REPL to view and save the plot.
<
using Plots
using Primes
Line 544:
y = map(g, 2x)
scatter(x, y, markerstrokewidth = 0, color = ["red", "blue", "green"][mod1.(x, 3)])
</
The first 100 G numbers are:
1 1 1 2 1 2 2 2 2 3
Line 561:
=={{header|Lua}}==
<
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))</
{{out}}
<pre>The first 100 G numbers:
Line 605:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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]</
{{out}}
<pre>1 1 1 2 1 2 2 2 2 3
Line 632:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 667:
binmode $fh;
print $fh $gd->png();
close $fh;</
{{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].
<!--<
<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>
<!--</
{{out}}
<pre>
Line 756:
=={{header|Picat}}==
<
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).</
{{out}}
Line 785:
=={{header|Python}}==
<
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()</
<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"
# Task
Line 851:
values => [@y,],
).plot: :points;
</syntaxhighlight>
{{out}}
<pre>The first 100 G values:
Line 870:
=={{header|Rust}}==
<
// primal = "0.3"
// plotters = "0.3.2"
Line 944:
Err(error) => eprintln!("Error: {}", error),
}
}</
{{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@)</
{{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.
<
import "graphics" for Canvas, Color
import "./math2" for Int
Line 1,126:
}
var Game = Main.new()</
{{out}}
Line 1,146:
=={{header|XPL0}}==
<syntaxhighlight 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);
]</
{{out}}
|