Gaussian primes: Difference between revisions

m
remove for editing
m (remove for editing)
Line 155:
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</lang>
 
=={{header|Julia}}==
`norm` in Julia, which for a complex number is the same as the square root of the `norm` in the task text, is used below.
<lang ruby>using LinearAlgebra
using Plots
using Primes
 
"""
function isGaussianprime(n::T) where T <: Integer
 
A Gaussian integer (in Julia, a Complex type) is a Gaussian prime if and only if either its norm
is a prime number, or it is the product of a unit (±1, ±i) and a prime number of the form 4n + 3.
"""
function isGaussianprime(n::T) where T <: Integer
r, c = abs(real(n)), imag(n)
return isprime(r * r + c * c) || r == abs(c) && isprime(r) && (r - 3) % 4 == 0
end
 
function testgaussprimes(lim = 10)
testvals = map(c -> c[1] + im * c[2], collect(Iterators.product(-lim:lim, -lim:lim)))
gprimes = sort!(filter(c -> isGaussianprime(c) && norm(c) < lim, testvals), by = norm)
println("Gaussian primes within $lim of the origin on the complex plane:")
foreach(p -> print(lpad(p[2], 10), p[1] % 10 == 0 ? "\n" : ""), enumerate(gprimes)) # print
scatter(gprimes) # plot
end
 
testgaussprimes()
</lang>{{out}}
<pre>
Gaussian primes within 10 of the origin on the complex plane:
-1 - 1im 1 - 1im -1 + 1im 1 + 1im 1 + 2im 1 - 2im -1 - 2im 2 - 1im -2 + 1im 2 + 1im
-1 + 2im -2 - 1im 3 - 2im -3 + 2im -3 - 2im 2 - 3im -2 - 3im -2 + 3im 2 + 3im 3 + 2im
1 - 4im 1 + 4im -1 + 4im 4 - 1im -4 + 1im -4 - 1im -1 - 4im 4 + 1im 3 + 3im -3 - 3im
-3 + 3im 3 - 3im 2 - 5im -2 - 5im -5 - 2im -5 + 2im -2 + 5im 5 + 2im 5 - 2im 2 + 5im
6 - 1im 6 + 1im -1 + 6im -6 + 1im 1 + 6im -1 - 6im 1 - 6im -6 - 1im 5 + 4im 4 + 5im
5 - 4im -5 - 4im -4 + 5im 4 - 5im -4 - 5im -5 + 4im 7 + 2im -2 - 7im 2 + 7im -2 + 7im
-7 + 2im -7 - 2im 7 - 2im 2 - 7im -6 + 5im 6 + 5im -5 + 6im 5 + 6im -5 - 6im 5 - 6im
-6 - 5im 6 - 5im 3 + 8im 8 + 3im 3 - 8im -3 + 8im -3 - 8im -8 - 3im 8 - 3im -8 + 3im
5 + 8im -8 - 5im 5 - 8im -8 + 5im 8 - 5im -5 - 8im -5 + 8im 8 + 5im -4 + 9im -4 - 9im
9 + 4im -9 + 4im 9 - 4im -9 - 4im 4 - 9im 4 + 9im -7 + 7im 7 + 7im 7 - 7im -7 - 7im
</pre>
 
=={{header|Phix}}==
4,102

edits