Gaussian primes: Difference between revisions
Content deleted Content added
Created Nim solution. |
Added Sidef |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 1:
{{
A [[wp:Gaussian Integer|Gaussian Integer]] is a complex number such that its real and imaginary parts are both integers.
Line 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}}==
Line 1,249 ⟶ 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|Sidef}}==
{{trans|Perl}}
<syntaxhighlight lang="ruby">func gaussianprimes(size) {
var plot = (2*size + 1 -> of { 2*size + 1 -> of(' ') })
var primes = []
for A in (-size .. size) {
var limit = isqrt(size**2 - A**2)
for B in (-limit .. limit) {
var n = Gauss(A, B)
if (n.is_prime) {
primes << n.to_n
plot[B + size + 1][A + size + 1] = 'X'
}
}
}
return (plot, primes)
}
with(10) {|n|
var(_plot, primes) = gaussianprimes(n)
say "Primes within #{n}:"
primes.slices(10).each { .map{'%7s'%_}.join(", ").say }
}
with (50) {|n|
var (plot) = gaussianprimes(n)
say "\nPlot within #{n}"
plot.each { .join('').say }
}</syntaxhighlight>
{{out}}
<pre>
Primes within 10:
-9 - 4i, -9 + 4i, -8 - 5i, -8 - 3i, -8 + 3i, -8 + 5i, -7 - 2i, -7, -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
-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, -7i, -3i
3i, 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 - 2i, 7, 7 + 2i, 8 - 5i, 8 - 3i, 8 + 3i, 8 + 5i, 9 - 4i, 9 + 4i
</pre>
The plot up to 50 is identical to the one produced by the Perl entry.
=={{header|Wren}}==
Line 1,257 ⟶ 1,367:
{{libheader|wren-fmt}}
Plots the points up to a radius of 150 to produce a similar image to the Raku example.
<syntaxhighlight lang="
import "graphics" for Canvas, Color
import "./plot" for Axes
|