How to resolve the algorithm Gaussian primes step by step in the jq programming language
How to resolve the algorithm Gaussian primes step by step in the jq programming language
Table of Contents
Problem Statement
A Gaussian Integer is a complex number such that its real and imaginary parts are both integers. The norm of a Gaussian integer is its product with its conjugate.
A Gaussian integer is a Gaussian prime if and only if either: both a and b are non-zero and its norm is a prime number, or, one of a or b is zero and it is the product of a unit (±1, ±i) and a prime integer of the form 4n + 3. Prime integers that are not of the form 4n + 3 can be factored into a Gaussian integer and its complex conjugate so are not a Gaussian prime. Gaussian primes are octogonally symmetrical on a real / imaginary Cartesian field. If a particular complex norm a² + b² is prime, since addition is commutative, b² + a² is also prime, as are the complex conjugates and negated instances of both.
Find and show, here on this page, the Gaussian primes with a norm of less than 100, (within a radius of 10 from the origin 0 + 0i on a complex plane.) Plot the points corresponding to the Gaussian primes on a Cartesian real / imaginary plane at least up to a radius of 50.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Gaussian primes step by step in the jq programming language
Source code in the jq programming language
### Complex numbers
# For clarity:
def real: first;
def imag: last;
# Complex or real
def norm:
def sq: .*.;
if type == "number" then sq
else map(sq) | add
end;
# Complex or real
def abs:
if type == "array" then norm | sqrt
elif . < 0 then - . else .
end;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
# Input should be an integer
def isPrime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
else 5
| until( . <= 0;
if .*. > $n then -1
elif ($n % . == 0) then 0
else . + 2
| if ($n % . == 0) then 0
else . + 4
end
end)
| . == -1
end;
# Given a stream of non-null values,
# group by the values of `x|filter` that occur in a run.
def runs(stream; filter):
foreach (stream, null) as $x ({emit: false, array: []};
if $x == null
then .emit = .array
elif .array == [] or ($x|filter) == (.array[0]|filter)
then .array += [$x] | .emit = false
else {emit: .array, array: [$x]}
end;
select(.emit).emit) ;
# emit a stream of Gaussian primes with real and imaginary parts within the given radius
def GPrimes($Radius):
def check: abs | isPrime and ((. - 3) % 4 == 0);
($Radius | norm) as $R2
| range(-$Radius; $Radius + 1) as $r
| range(-$Radius; $Radius + 1) as $i
| [$r, $i]
| norm as $norm
| select( $norm < $R2 )
| if $i == 0 then select($r|check)
elif $r == 0 then select($i|check)
else select($norm|isPrime)
end ;
def plot($Radius):
"# X Y",
(GPrimes($Radius) | "\(first) \(last)");
# For the first task:
"Gaussian primes with norm < 100, grouped by the real part:",
(runs(GPrimes(10); real) | map(tostring) | join(" "))
# For the plot:
# plot(100)
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the Delphi programming language
You may also check:How to resolve the algorithm Variable size/Set step by step in the PL/I programming language
You may also check:How to resolve the algorithm N-smooth numbers step by step in the Python programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the FunL programming language
You may also check:How to resolve the algorithm Gray code step by step in the PicoLisp programming language