Gaussian primes: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 36: | Line 36: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Gaussian primes. Nigel Galloway: July 29th., 2022 |
// 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) |
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 "" |
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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 46: | Line 46: | ||
</pre> |
</pre> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Implementation: < |
Implementation: <syntaxhighlight lang="j">isgpri=: {{ |
||
if. 1 p: (*+) y do. 1 return. end. |
if. 1 p: (*+) y do. 1 return. end. |
||
int=. |(+.y)-.0 |
int=. |(+.y)-.0 |
||
if. 1=#int do. {.(1 p: int) * 3=4|int else. 0 end. |
if. 1=#int do. {.(1 p: int) * 3=4|int else. 0 end. |
||
}}"0</ |
}}"0</syntaxhighlight> |
||
[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.) |
[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: < |
Plot of gaussian primes up to radius 50: <syntaxhighlight lang="j"> 1j1#"1'#' (<"1]50++.(#~ isgpri * 50>:|) ,j./~i:100)} '+' (<50 50)} '|' 50}"1 '-' 50} 100 100$' '</syntaxhighlight> |
||
<pre style="font-size:60%;"> | |
<pre style="font-size:60%;"> | |
||
# # | # # |
# # | # # |
||
Line 157: | Line 157: | ||
</pre> |
</pre> |
||
Gaussian primes less than radius 10 (sorted by radius):< |
Gaussian primes less than radius 10 (sorted by radius):<syntaxhighlight lang="j"> 10 10$(/: |)(#~ isgpri * 10>|) ,j./~i:10 |
||
_1j_1 _1j1 1j_1 1j1 _2j_1 _2j1 _1j_2 _1j2 1j_2 1j2 |
_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 |
2j_1 2j1 _3 0j_3 0j3 3 _3j_2 _3j2 _2j_3 _2j3 |
||
Line 167: | Line 167: | ||
_5j_6 _5j6 5j_6 5j6 6j_5 6j5 _8j_3 _8j3 _3j_8 _3j8 |
_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 |
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</ |
8j_5 8j5 _9j_4 _9j4 _4j_9 _4j9 4j_9 4j9 9j_4 9j4</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="ruby">using LinearAlgebra |
||
using Plots |
using Plots |
||
using Primes |
using Primes |
||
Line 200: | Line 200: | ||
testgaussprimes() |
testgaussprimes() |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Gaussian primes within 10 of the origin on the complex plane: |
Gaussian primes within 10 of the origin on the complex plane: |
||
Line 216: | Line 216: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">n = 100; |
||
digs = Reap@Do[If[Norm[i + I j]^2 < n, If[PrimeQ[i + I j, GaussianIntegers -> True], Sow[i + I j]]], |
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]]} |
{i,Floor[-Sqrt[n]], Ceiling[Sqrt[n]]}, {j, Floor[-Sqrt[n]], Ceiling[Sqrt[n]]} |
||
Line 228: | Line 228: | ||
digs //= Map[StringJoin]; |
digs //= Map[StringJoin]; |
||
digs //= StringRiffle[#, "\n"] &; |
digs //= StringRiffle[#, "\n"] &; |
||
digs</ |
digs</syntaxhighlight> |
||
{{out}} |
{{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 |
<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: | Line 346: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Gaussian_primes |
use strict; # https://rosettacode.org/wiki/Gaussian_primes |
||
Line 378: | Line 378: | ||
} |
} |
||
return $plot, @primes; |
return $plot, @primes; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Primes within 10 |
<pre>Primes within 10 |
||
Line 498: | Line 498: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/gp.htm here]. |
You can run this online [http://phix.x10.mx/p2js/gp.htm here]. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo/rosetta/Gaussian_primes.exw |
-- demo/rosetta/Gaussian_primes.exw |
||
Line 610: | Line 610: | ||
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
Output same as Raku |
Output same as Raku |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">''' python example for task rosettacode.org/wiki/Gaussian_primes ''' |
||
from matplotlib.pyplot import scatter |
from matplotlib.pyplot import scatter |
||
Line 651: | Line 651: | ||
print(str(c).ljust(9), end='\n' if (i +1) % 10 == 0 else '') |
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]) |
scatter([c.real for c in gprimes], [c.imag for c in gprimes]) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Gaussian primes within 10 of the origin on the complex plane: |
Gaussian primes within 10 of the origin on the complex plane: |
||
Line 668: | Line 668: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Plotting the points up to a radius of 150. |
Plotting the points up to a radius of 150. |
||
<lang |
<syntaxhighlight lang="raku" line>use List::Divvy; |
||
my @next = { :1x, :1y, :2n },; |
my @next = { :1x, :1y, :2n },; |
||
Line 715: | Line 715: | ||
|@points |
|@points |
||
], |
], |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Gaussian primes with a norm less than 100 sorted by norm: |
<pre>Gaussian primes with a norm less than 100 sorted by norm: |
||
Line 738: | Line 738: | ||
{{libheader|wren-fmt}} |
{{libheader|wren-fmt}} |
||
Plots the points up to a radius of 150 to produce a similar image to the Raku example. |
Plots the points up to a radius of 150 to produce a similar image to the Raku example. |
||
< |
<syntaxhighlight lang="ecmascript">import "dome" for Window |
||
import "graphics" for Canvas, Color |
import "graphics" for Canvas, Color |
||
import "./plot" for Axes |
import "./plot" for Axes |
||
Line 793: | Line 793: | ||
} |
} |
||
var Game = Main.new()</ |
var Game = Main.new()</syntaxhighlight> |
||
{{out}} |
{{out}} |