Gaussian primes: Difference between revisions

m (→‎{{header|Phix}}: (modified cdCanvasPixel to use sfillRect instead of strokeRect, much closer to desktop now))
Line 320:
<!--</lang>-->
Output same as Raku
 
=={{header|Python}}==
<lang python>''' python example for task rosettacode.org/wiki/Gaussian_primes '''
 
from matplotlib.pyplot import scatter
from sympy import isprime
from math import isqrt
 
''' Task complex norm function '''
def norm(c):
return c.real * c.real + c.imag * c.imag
 
 
def is_gaussian_prime(n):
'''
is_gaussian_prime(n)
A Gaussian prime is a non-unit Gaussian integer m + ni divisible only by its associates and by the units
1, i, -1, -i and by no other Gaussian integers.
The Gaussian primes fall into one of three categories:
Gaussian integers with imaginary part zero and a prime real part m with |m| a real prime satisfying |m| = 3 mod 4
Gaussian integers with real part zero and an imaginary part n with |n| real prime satisfying |n| = 3 mod 4
Gaussian integers having both real and imaginary parts, and its complex norm (square of algebraic norm) is a real prime number
'''
r, c = int(abs(n.real)), int(abs(n.imag))
return isprime(r * r + c * c) or c == 0 and isprime(r) and (r - 3) % 4 == 0 or r == 0 and isprime(c) and (c - 3) % 4 == 0
 
if __name__ == '__main__':
 
limitsquared = 100
lim = isqrt(limitsquared)
testvals = [complex(r, c) for r in range(-lim, lim) for c in range(-lim, lim)]
gprimes = sorted(filter(lambda c : is_gaussian_prime(c) and norm(c) < limitsquared, testvals), key=norm)
print(f'Gaussian primes within {isqrt(limitsquared)} of the origin on the complex plane:')
for i, c in enumerate(gprimes):
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])
</lang>{{out}}
<pre>
Gaussian primes within 10 of the origin on the complex plane:
(-1-1j) (-1+1j) (1-1j) (1+1j) (-2-1j) (-2+1j) (-1-2j) (-1+2j) (1-2j) (1+2j)
(2-1j) (2+1j) (-3+0j) -3j 3j (3+0j) (-3-2j) (-3+2j) (-2-3j) (-2+3j)
(2-3j) (2+3j) (3-2j) (3+2j) (-4-1j) (-4+1j) (-1-4j) (-1+4j) (1-4j) (1+4j)
(4-1j) (4+1j) (-5-2j) (-5+2j) (-2-5j) (-2+5j) (2-5j) (2+5j) (5-2j) (5+2j)
(-6-1j) (-6+1j) (-1-6j) (-1+6j) (1-6j) (1+6j) (6-1j) (6+1j) (-5-4j) (-5+4j)
(-4-5j) (-4+5j) (4-5j) (4+5j) (5-4j) (5+4j) (-7+0j) -7j 7j (7+0j)
(-7-2j) (-7+2j) (-2-7j) (-2+7j) (2-7j) (2+7j) (7-2j) (7+2j) (-6-5j) (-6+5j)
(-5-6j) (-5+6j) (5-6j) (5+6j) (6-5j) (6+5j) (-8-3j) (-8+3j) (-3-8j) (-3+8j)
(3-8j) (3+8j) (8-3j) (8+3j) (-8-5j) (-8+5j) (-5-8j) (-5+8j) (5-8j) (5+8j)
(8-5j) (8+5j) (-9-4j) (-9+4j) (-4-9j) (-4+9j) (4-9j) (4+9j) (9-4j) (9+4j)
</pre>
 
=={{header|Raku}}==
4,102

edits