Gaussian primes: Difference between revisions

added RPL
(Added Quackery.)
(added RPL)
 
(10 intermediate revisions by 7 users not shown)
Line 1:
{{draft task}}
 
A [[wp:Gaussian Integer|Gaussian Integer]] is a complex number such that its real and imaginary parts are both integers.
Line 33:
 
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F is_prime(a)
I a == 2
R 1B
I a < 2 | a % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(a))).step(2)
I a % i == 0
R 0B
R 1B
 
F is_gaussian_prime(n)
V (r, c) = (Int(abs(n.real)), Int(abs(n.imag)))
R is_prime(r * r + c * c) | (c == 0 & is_prime(r) & (r - 3) % 4 == 0) | (r == 0 & is_prime(c) & (c - 3) % 4 == 0)
 
F norm(c)
R c.real * c.real + c.imag * c.imag
 
V limitsquared = 100
V lim = Int(sqrt(limitsquared))
V testvals = multiloop((-lim .< lim), (-lim .< lim), (r, c) -> Complex(r, c))
testvals .= filter(c -> is_gaussian_prime(c) & norm(c) < :limitsquared)
V gprimes = sorted(enumerate(testvals).map((i, c) -> (c, i)), key' c -> (norm(c[0]), c[1]))
print(‘Gaussian primes within ’lim‘ of the origin on the complex plane:’)
L(c) gprimes
print(String(c[0]).ljust(9), end' I (L.index + 1) % 10 == 0 {"\n"} E ‘’)
</syntaxhighlight>
 
{{out}}
<pre>
Gaussian primes within 10 of the origin on the complex plane:
-1-1i -1+1i 1-1i 1+1i -2-1i -2+1i -1-2i -1+2i 1-2i 1+2i
2-1i 2+1i -3 -3i 3i 3 -3-2i -3+2i -2-3i -2+3i
2-3i 2+3i 3-2i 3+2i -4-1i -4+1i -1-4i -1+4i 1-4i 1+4i
4-1i 4+1i -5-2i -5+2i -2-5i -2+5i 2-5i 2+5i 5-2i 5+2i
-6-1i -6+1i -1-6i -1+6i 1-6i 1+6i 6-1i 6+1i -5-4i -5+4i
-4-5i -4+5i 4-5i 4+5i 5-4i 5+4i -7 -7i 7i 7
-7-2i -7+2i -2-7i -2+7i 2-7i 2+7i 7-2i 7+2i -6-5i -6+5i
-5-6i -5+6i 5-6i 5+6i 6-5i 6+5i -8-3i -8+3i -3-8i -3+8i
3-8i 3+8i 8-3i 8+3i -8-5i -8+5i -5-8i -5+8i 5-8i 5+8i
8-5i 8+5i -9-4i -9+4i -4-9i -4+9i 4-9i 4+9i 9-4i 9+4i
</pre>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
The ALGOL 68-primes source is on a page in Rosetta Code - see the link above.
<syntaxhighlight lang="algol68">
BEGIN # find and plot Gaussian primes: complex numbers with integer real and #
# imaginary parts where: #
# either the real and imaginary parts are non 0 and the norm #
# (sum of squares of the real and imaginary parts) is prime #
# or one of the real/imaginary parts is zero and the other is #
# +/- a prime of the form 4n + 3 #
 
PR read "primes.incl.a68" PR # include prime utilities #
[]BOOL primes = PRIMESIEVE 10 000; # sieve the primes to 10 000 #
 
# applies gp to the Gaussian primes with within radius of 0+0i #
PROC process gaussian primes = ( INT radius, PROC(INT,INT)VOID gp )VOID:
BEGIN
INT r2 = radius * radius;
FOR rp FROM - radius TO radius DO
FOR ip FROM - radius TO radius DO
IF INT norm = ( rp * rp ) + ( ip * ip );
norm < r2
THEN
IF rp /= 0 AND ip /= 0 THEN
IF primes[ norm ] THEN gp( rp, ip ) FI
ELIF rp /= 0 OR ip /= 0 THEN
IF INT abs sum parts = ABS ( rp + ip );
primes[ abs sum parts ]
AND ( abs sum parts - 3 ) MOD 4 = 0
THEN
gp( rp, ip )
FI
FI
FI
OD
OD
END # process gaussian primes # ;
 
# show Gaussian primes within a radius (root of the norm) of 10 of 0+0i #
INT gp count := 0;
process gaussian primes( 10
, ( INT rp, ip )VOID:
BEGIN
print( ( " "
, IF rp > 0 THEN " " ELSE "-" FI, whole( ABS rp, 0 )
, IF ip < 0 THEN "-" ELSE "+" FI, whole( ABS ip, 0 )
, "i"
)
),
IF ( gp count +:= 1 ) MOD 12 = 0 THEN print( ( newline ) ) FI
END
);
# plot the Gaussian primes within a radius of 50 of the origin (0+0i) #
[ -50 : 50, -50 : 50 ]CHAR plot;
FOR i FROM 1 LWB plot TO 1 UPB plot DO
FOR j FROM 2 LWB plot TO 2 UPB plot DO
plot[ i, j ] := IF i = 0 THEN "-" ELSE " " FI
OD;
plot[ i, 0 ] := "|"
OD;
plot[ 0, 0 ] := "+";
process gaussian primes( 50, ( INT rp, ip )VOID: plot[ rp, ip ] := "*" );
print( ( newline, newline ) );
FOR i FROM 1 LWB plot TO 1 UPB plot DO
FOR j FROM 2 LWB plot TO 2 UPB plot DO
print( ( " ", plot[ i, j ] ) )
OD;
print( ( newline ) )
OD
END
</syntaxhighlight>
{{out}}
<pre>
-9-4i -9+4i -8-5i -8-3i -8+3i -8+5i -7-2i -7+0i -7+2i -6-5i -6-1i -6+1i
-6+5i -5-8i -5-6i -5-4i -5-2i -5+2i -5+4i -5+6i -5+8i -4-9i -4-5i -4-1i
-4+1i -4+5i -4+9i -3-8i -3-2i -3+0i -3+2i -3+8i -2-7i -2-5i -2-3i -2-1i
-2+1i -2+3i -2+5i -2+7i -1-6i -1-4i -1-2i -1-1i -1+1i -1+2i -1+4i -1+6i
-0-7i -0-3i -0+3i -0+7i 1-6i 1-4i 1-2i 1-1i 1+1i 1+2i 1+4i 1+6i
2-7i 2-5i 2-3i 2-1i 2+1i 2+3i 2+5i 2+7i 3-8i 3-2i 3+0i 3+2i
3+8i 4-9i 4-5i 4-1i 4+1i 4+5i 4+9i 5-8i 5-6i 5-4i 5-2i 5+2i
5+4i 5+6i 5+8i 6-5i 6-1i 6+1i 6+5i 7-2i 7+0i 7+2i 8-5i 8-3i
8+3i 8+5i 9-4i 9+4i
</pre>
<pre style="font-size:60%;">
|
|
* * | * *
* | *
* * * * * * *
* * * * | * * * *
* * * * | * * * *
* * * * | * * * *
* * * * * * * * *
* * * * * | * * * * *
* * * * | * * * *
* * * * * * | * * * * * *
* * | * *
* * * * * * * | * * * * * * *
* * * * * * | * * * * * *
* * * * | * * * *
* * * * * * * * | * * * * * * * *
* * * * * * * | * * * * * * *
* * * * * | * * * * *
* * * * * * * * | * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * | * * * * * *
* * * * * * * * | * * * * * * * *
* * * * * * | * * * * * *
* * * * * | * * * * *
* * * * * * * * * | * * * * * * * * *
* * * * * * * * * * * | * * * * * * * * * * *
* * * * * | * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * | * * * * * * * * *
* * * * * | * * * * *
* * * * * * * * * * * | * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * | * * * * * *
* * * * * * * * * | * * * * * * * * *
* * * * * * * * * * | * * * * * * * * * *
* * * * * * * * * * | * * * * * * * * * *
* * * * * * * * | * * * * * * * *
* * * * * * * * * * * * | * * * * * * * * * * * *
* * * * * * | * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * | * * * * * * * * * * * * * *
* * * * * * * | * * * * * * *
* * * * * * * * * * * * | * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * | * * * * * * * *
* * * * * * * * * * * * * * * | * * * * * * * * * * * * * * *
* * * * * * * * * * * | * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * | * * * * * * * * * * * * *
* * * * * * * * * * * * | * * * * * * * * * * * *
- - - * - - - * - - - - - - - - - - - * - - - - - - - * - - - * - - - - - - - * - - - * - - - * - - + - - * - - - * - - - * - - - - - - - * - - - * - - - - - - - * - - - - - - - - - - - * - - - * - - -
* * * * * * * * * * * * | * * * * * * * * * * * *
* * * * * * * * * * * * * | * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * | * * * * * * * * * * *
* * * * * * * * * * * * * * * | * * * * * * * * * * * * * * *
* * * * * * * * | * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * | * * * * * * * * * * * *
* * * * * * * | * * * * * * *
* * * * * * * * * * * * * * | * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * | * * * * * *
* * * * * * * * * * * * | * * * * * * * * * * * *
* * * * * * * * | * * * * * * * *
* * * * * * * * * * | * * * * * * * * * *
* * * * * * * * * * | * * * * * * * * * *
* * * * * * * * * | * * * * * * * * *
* * * * * * | * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * | * * * * * * * * * * *
* * * * * | * * * * *
* * * * * * * * * | * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * *
* * * * * | * * * * *
* * * * * * * * * * * | * * * * * * * * * * *
* * * * * * * * * | * * * * * * * * *
* * * * * | * * * * *
* * * * * * | * * * * * *
* * * * * * * * | * * * * * * * *
* * * * * * | * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * | * * * * * * * *
* * * * * | * * * * *
* * * * * * * | * * * * * * *
* * * * * * * * | * * * * * * * *
* * * * | * * * *
* * * * * * | * * * * * *
* * * * * * * | * * * * * * *
* * | * *
* * * * * * | * * * * * *
* * * * | * * * *
* * * * * | * * * * *
* * * * * * * * *
* * * * | * * * *
* * * * | * * * *
* * * * | * * * *
* * * * * * *
* | *
* * | * *
|
</pre>
 
=={{header|F_Sharp|F#}}==
Line 45 ⟶ 279:
-9-4i -9+4i -8-7i -8-5i -8-3i -8+3i -8+5i -8+7i -7-8i -7-2i -7 -7+2i -7+8i -6-5i -6-1i -6+1i -6+5i -5-8i -5-6i -5-4i -5-2i -5+2i -5+4i -5+6i -5+8i -4-9i -4-5i -4-1i -4+1i -4+5i -4+9i -3-8i -3-2i -3 -3+2i -3+8i -2-7i -2-5i -2-3i -2-1i -2+1i -2+3i -2+5i -2+7i -1-6i -1-4i -1-2i -1-1i -1+1i -1+2i -1+4i -1+6i 0-7i 0-3i 0+3i 0+7i 1-6i 1-4i 1-2i 1-1i 1+1i 1+2i 1+4i 1+6i 2-7i 2-5i 2-3i 2-1i 2+1i 2+3i 2+5i 2+7i 3-8i 3-2i 3 3+2i 3+8i 4-9i 4-5i 4-1i 4+1i 4+5i 4+9i 5-8i 5-6i 5-4i 5-2i 5+2i 5+4i 5+6i 5+8i 6-5i 6-1i 6+1i 6+5i 7-8i 7-2i 7 7+2i 7+8i 8-7i 8-5i 8-3i 8+3i 8+5i 8+7i 9-4i 9+4i
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">'#include "isprime.bas"
 
Function isGaussianPrime(realPart As Integer, imagPart As Integer) As Boolean
Dim As Integer norm = realPart * realPart + imagPart * imagPart
If realPart = 0 Andalso imagPart <> 0 Then
Return (Abs(imagPart) Mod 4 = 3) Andalso isPrime(Abs(imagPart))
Elseif imagPart = 0 And realPart <> 0 Then
Return (Abs(realPart) Mod 4 = 3) Andalso isPrime(Abs(realPart))
Else
Return isPrime(norm)
End If
End Function
 
Print "Gaussian primes within 10 of the origin on the complex plane:"
Dim As Integer i, j, norm, radius = 50
For i = -radius To radius
For j = -radius To radius
norm = i * i + j * j
If norm < 100 Andalso isGaussianPrime(i, j) Then
Print Using "##"; i;
Print Iif(j >= 0, "+", "");
Print j & "i",
End If
Next j
Next i
 
Sleep</syntaxhighlight>
 
=={{header|J}}==
Implementation: <syntaxhighlight lang="j">isgpri=: {{
Line 461 ⟶ 725:
* * * *
</pre>
 
=={{header|Nim}}==
{{libheader|gnuplotlib.nim}}
<syntaxhighlight lang="Nim">import std/[algorithm, math, strformat]
import gnuplot
 
type IntComplex = tuple[re, im: int]
 
func isPrime(n: Natural): bool =
if n < 2: return false
if (n and 1) == 0: return n == 2
var d = 3
while d * d <= n:
if n mod d == 0:
return false
inc d, 2
result = true
 
func norm(c: IntComplex): Natural =
c.re * c.re + c.im * c.im
 
func `$`(c: IntComplex): string =
if c.im == 0: return $c.re
if c.re == 0: return $c.im & 'i'
let op = if c.im > 0: '+' else: '-'
result = &"{c.re}{op}{abs(c.im)}i"
 
func isGaussianPrime(c: IntComplex): bool =
if c.re == 0:
let x = abs(c.im)
return x.isPrime and (x and 3) == 3
if c.im == 0:
let x = abs(c.re)
return x.isPrime and (x and 3) == 3
result = c.norm.isPrime
 
func gaussianPrimes(maxNorm: Positive): seq[IntComplex] =
var gpList: seq[IntComplex]
let m = sqrt(maxNorm.toFloat).int
for x in -m..m:
for y in -m..m:
let c = (x, y)
if c.norm < maxNorm and c.isGaussianPrime:
gpList.add c
result = gpList.sortedByIt(it.norm)
 
echo "Gaussian primes with a norm less than 100 sorted by norm:"
for i, gp in gaussianPrimes(100):
stdout.write &"{gp:>5}"
stdout.write if i mod 10 == 9: '\n' else: ' '
 
var x, y: seq[int]
for gp in gaussianPrimes(150^2):
x.add gp.re
y.add gp.im
 
withGnuPlot:
cmd "set size ratio -1"
plot(x, y, "Gaussian primes", "with dots lw 2")
</syntaxhighlight>
 
{{out}}
<pre>Gaussian primes with a norm less than 100 sorted by norm:
-1-1i -1+1i 1-1i 1+1i -2-1i -2+1i -1-2i -1+2i 1-2i 1+2i
2-1i 2+1i -3 -3i 3i 3 -3-2i -3+2i -2-3i -2+3i
2-3i 2+3i 3-2i 3+2i -4-1i -4+1i -1-4i -1+4i 1-4i 1+4i
4-1i 4+1i -5-2i -5+2i -2-5i -2+5i 2-5i 2+5i 5-2i 5+2i
-6-1i -6+1i -1-6i -1+6i 1-6i 1+6i 6-1i 6+1i -5-4i -5+4i
-4-5i -4+5i 4-5i 4+5i 5-4i 5+4i -7 -7i 7i 7
-7-2i -7+2i -2-7i -2+7i 2-7i 2+7i 7-2i 7+2i -6-5i -6+5i
-5-6i -5+6i 5-6i 5+6i 6-5i 6+5i -8-3i -8+3i -3-8i -3+8i
3-8i 3+8i 8-3i 8+3i -8-5i -8+5i -5-8i -5+8i 5-8i 5+8i
8-5i 8+5i -9-4i -9+4i -4-9i -4+9i 4-9i 4+9i 9-4i 9+4i
</pre>
 
=={{header|Perl}}==
Line 850 ⟶ 1,188:
 
turtle
0 frames
axes
100 ^2 1+ times
Line 857 ⟶ 1,196:
2drop done
2dup gprime iff
plot else 2drop ]</syntaxhighlight>
1 frames</syntaxhighlight>
 
{{out}}
Line 938 ⟶ 1,278:
 
Off-site SVG image: [https://raw.githubusercontent.com/thundergnat/rc/master/img/gaussian-primes-raku.svg gaussian-primes-raku.svg]
 
=={{header|RPL}}==
« → radius
« { }
1 radius '''FOR''' a
0 a '''FOR''' b
'''IF''' a SQ b SQ + radius SQ ≤ '''THEN'''
{ }
'''IF''' b '''THEN'''
'''IF''' a SQ b SQ + ISPRIME? '''THEN'''
a b R→C DUP CONJ OVER NEG DUP CONJ 4 →LIST +
'''END'''
'''ELSE'''
'''IF''' a ISPRIME? a 4 MOD 3 == AND '''THEN'''
a DUP NEG 2 →LIST +
'''END'''
'''END'''
'''IF''' DUP SIZE '''THEN '''
'''IF''' a b ≠ '''THEN''' DUP (0,1) * + '''END'''
+
'''ELSE''' DROP '''END'''
'''END'''
'''NEXT NEXT '''
» » '<span style="color:blue">GPRIMES</span>' STO
« 10 <span style="color:blue">GPRIMES</span>
(-50 -50) PMIN (50 50) PMAX ERASE
50 <span style="color:blue">GPRIMES</span> 1 « '''IF''' DUP IM NOT '''THEN''' 0 R→C '''END''' PIXON » DOLIST
{ } PVIEW
» '<span style="color:blue">TASK</span>' STO
[[File:Gaussian primes.png|thumb|alt=Gaussian primes within radius 50|HP-48G emulator screenshot]]
{{out}}
<pre>
1: { (1.,1.) (1.,-1.) (-1.,-1.) (-1.,1.) (2.,1.) (2.,-1.) (-2.,-1.) (-2.,1.) (-1.,2.) (1.,2.) (1.,-2.) (-1.,-2.) 3 -3 (0.,3.) (0.,-3.) (3.,2.) (3.,-2.) (-3.,-2.) (-3.,2.) (-2.,3.) (2.,3.) (2.,-3.) (-2.,-3.) (4.,1.) (4.,-1.) (-4.,-1.) (-4.,1.) (-1.,4.) (1.,4.) (1.,-4.) (-1.,-4.) (5.,2.) (5.,-2.) (-5.,-2.) (-5.,2.) (-2.,5.) (2.,5.) (2.,-5.) (-2.,-5.) (5.,4.) (5.,-4.) (-5.,-4.) (-5.,4.) (-4.,5.) (4.,5.) (4.,-5.) (-4.,-5.) (6.,1.) (6.,-1.) (-6.,-1.) (-6.,1.) (-1.,6.) (1.,6.) (1.,-6.) (-1.,-6.) (6.,5.) (6.,-5.) (-6.,-5.) (-6.,5.) (-5.,6.) (5.,6.) (5.,-6.) (-5.,-6.) 7 -7 (0.,7.) (0.,-7.) (7.,2.) (7.,-2.) (-7.,-2.) (-7.,2.) (-2.,7.) (2.,7.) (2.,-7.) (-2.,-7.) (8.,3.) (8.,-3.) (-8.,-3.) (-8.,3.) (-3.,8.) (3.,8.) (3.,-8.) (-3.,-8.) (8.,5.) (8.,-5.) (-8.,-5.) (-8.,5.) (-5.,8.) (5.,8.) (5.,-8.) (-5.,-8.) (9.,4.) (9.,-4.) (-9.,-4.) (-9.,4.) (-4.,9.) (4.,9.) (4.,-9.) (-4.,-9.) }
</pre>
 
=={{header|Wren}}==
Line 946 ⟶ 1,321:
{{libheader|wren-fmt}}
Plots the points up to a radius of 150 to produce a similar image to the Raku example.
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color
import "./plot" for Axes
1,150

edits