Gaussian primes: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 36:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Gaussian primes. Nigel Galloway: July 29th., 2022
let isGP=function (n,0)|(0,n)->let n=abs n in n%4=3 && isPrime n |(n,g)->isPrime(n*n+g*g)
Seq.allPairs [-9..9] [-9..9]|>Seq.filter isGP|>Seq.iter(fun(n,g)->printf $"""%d{n}%s{match g with 0->"" |g->sprintf $"%+d{g}i"} """); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 46:
</pre>
=={{header|J}}==
Implementation: <langsyntaxhighlight Jlang="j">isgpri=: {{
if. 1 p: (*+) y do. 1 return. end.
int=. |(+.y)-.0
if. 1=#int do. {.(1 p: int) * 3=4|int else. 0 end.
}}"0</langsyntaxhighlight>
 
[https://jsoftware.github.io/j-playground/bin/html2/#code=isgpri%3D%3A%20%7B%7B%0A%20%20if.%201%20p%3A%20%28*%2B%29%20y%20do.%201%20return.%20end.%0A%20%20int%3D.%20%7C%28%2B.y%29-.0%0A%20%20if.%201%3D%23int%20do.%20%7B.%281%20p%3A%20int%29%20*%203%3D4%7Cint%20else.%200%20end.%0A%7D%7D%220%0A%0Arequire'viewmat'%0A%0Aviewmat%20%28isgpri%20*%20R%3E%3A%7C%29j.%2F~i%3AR%3D%3A%20100 Online plot of gaussian primes up to radius 100]. (Hit "Run" in the upper right-hand corner.)
 
Plot of gaussian primes up to radius 50: <langsyntaxhighlight Jlang="j"> 1j1#"1'#' (<"1]50++.(#~ isgpri * 50>:|) ,j./~i:100)} '+' (<50 50)} '|' 50}"1 '-' 50} 100 100$' '</langsyntaxhighlight>
<pre style="font-size:60%;"> |
# # | # #
Line 157:
</pre>
 
Gaussian primes less than radius 10 (sorted by radius):<langsyntaxhighlight Jlang="j"> 10 10$(/: |)(#~ isgpri * 10>|) ,j./~i:10
_1j_1 _1j1 1j_1 1j1 _2j_1 _2j1 _1j_2 _1j2 1j_2 1j2
2j_1 2j1 _3 0j_3 0j3 3 _3j_2 _3j2 _2j_3 _2j3
Line 167:
_5j_6 _5j6 5j_6 5j6 6j_5 6j5 _8j_3 _8j3 _3j_8 _3j8
3j_8 3j8 8j_3 8j3 _8j_5 _8j5 _5j_8 _5j8 5j_8 5j8
8j_5 8j5 _9j_4 _9j4 _4j_9 _4j9 4j_9 4j9 9j_4 9j4</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="ruby">using LinearAlgebra
using Plots
using Primes
Line 200:
 
testgaussprimes()
</langsyntaxhighlight>{{out}}
<pre>
Gaussian primes within 10 of the origin on the complex plane:
Line 216:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">n = 100;
digs = Reap@Do[If[Norm[i + I j]^2 < n, If[PrimeQ[i + I j, GaussianIntegers -> True], Sow[i + I j]]],
{i,Floor[-Sqrt[n]], Ceiling[Sqrt[n]]}, {j, Floor[-Sqrt[n]], Ceiling[Sqrt[n]]}
Line 228:
digs //= Map[StringJoin];
digs //= StringRiffle[#, "\n"] &;
digs</langsyntaxhighlight>
{{out}}
<pre>-9-4 I -9+4 I -8-5 I -8-3 I -8+3 I -8+5 I -7-2 I -7 -7+2 I -6-5 I
Line 346:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Gaussian_primes
Line 378:
}
return $plot, @primes;
}</langsyntaxhighlight>
{{out}}
<pre>Primes within 10
Line 498:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/gp.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo/rosetta/Gaussian_primes.exw
Line 610:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
Output same as Raku
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">''' python example for task rosettacode.org/wiki/Gaussian_primes '''
 
from matplotlib.pyplot import scatter
Line 651:
print(str(c).ljust(9), end='\n' if (i +1) % 10 == 0 else '')
scatter([c.real for c in gprimes], [c.imag for c in gprimes])
</langsyntaxhighlight>{{out}}
<pre>
Gaussian primes within 10 of the origin on the complex plane:
Line 668:
=={{header|Raku}}==
Plotting the points up to a radius of 150.
<syntaxhighlight lang="raku" perl6line>use List::Divvy;
 
my @next = { :1x, :1y, :2n },;
Line 715:
|@points
],
);</langsyntaxhighlight>
{{out}}
<pre>Gaussian primes with a norm less than 100 sorted by norm:
Line 738:
{{libheader|wren-fmt}}
Plots the points up to a radius of 150 to produce a similar image to the Raku example.
<langsyntaxhighlight lang="ecmascript">import "dome" for Window
import "graphics" for Canvas, Color
import "./plot" for Axes
Line 793:
}
 
var Game = Main.new()</langsyntaxhighlight>
 
{{out}}
10,327

edits