Gaussian primes: Difference between revisions

julia example
m (remove for editing)
(julia example)
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}}==
<lang ruby>using LinearAlgebra
using Plots
using Primes
 
"""
function isGaussianprime(n::Complex{T}) where T <: Integer
 
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
"""
function isGaussianprime(n::Complex{T}) where T <: Integer
r, c = abs(real(n)), abs(imag(n))
return isprime(r * r + c * c) || c == 0 && isprime(r) && (r - 3) % 4 == 0 || r == 0 && isprime(c) && (c - 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 = x -> norm(x)^2)
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 -2 + 1im 2 + 1im 2 - 1im -2 - 1im 1 - 2im
-1 - 2im -1 + 2im 3 + 0im -3 + 0im 0 - 3im 0 + 3im -3 - 2im -2 + 3im 3 + 2im 3 - 2im
-2 - 3im 2 + 3im 2 - 3im -3 + 2im 4 + 1im 4 - 1im -1 + 4im -4 - 1im -4 + 1im -1 - 4im
1 - 4im 1 + 4im 5 - 2im 2 + 5im -5 + 2im -5 - 2im 5 + 2im -2 + 5im 2 - 5im -2 - 5im
1 - 6im -6 + 1im 6 + 1im -6 - 1im -1 - 6im -1 + 6im 1 + 6im 6 - 1im -4 + 5im 5 + 4im
-5 + 4im 4 + 5im 5 - 4im -5 - 4im 4 - 5im -4 - 5im 0 + 7im -7 + 0im 0 - 7im 7 + 0im
7 + 2im -2 + 7im -2 - 7im 2 - 7im 2 + 7im 7 - 2im -7 - 2im -7 + 2im 6 - 5im -6 - 5im
5 + 6im -5 - 6im 5 - 6im -6 + 5im -5 + 6im 6 + 5im 3 + 8im -8 + 3im 8 + 3im -3 + 8im
-8 - 3im 8 - 3im 3 - 8im -3 - 8im 8 + 5im -5 - 8im -5 + 8im 5 - 8im -8 + 5im -8 - 5im
8 - 5im 5 + 8im -4 + 9im -4 - 9im 9 + 4im -9 + 4im 9 - 4im -9 - 4im 4 - 9im 4 + 9im
</pre>
 
 
=={{header|Phix}}==
4,102

edits