Gaussian primes: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Tweak following further changes to 'fmt'.) |
|||
Line 168: | Line 168: | ||
3j_8 3j8 8j_3 8j3 _8j_5 _8j5 _5j_8 _5j8 5j_8 5j8 |
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</syntaxhighlight> |
8j_5 8j5 _9j_4 _9j4 _4j_9 _4j9 4j_9 4j9 9j_4 9j4</syntaxhighlight> |
||
=={{header|jq}}== |
|||
In this entry, complex numbers are uniformly represented by a two-element array, [real, imaginary], and the function Gprimes(r) emits the Gaussian primes with norm less than r. For the first task, they are displayed in groups, with each group having the same real part. |
|||
To use gnuplot to create a plot of the Gaussian primes with norm less than 100, run |
|||
the program with `plot(100)` (using the jq options `-nr`) to create a file, say gaussian.dat, |
|||
and then run the gnuplot command: |
|||
<pre> |
|||
plot "gaussian.dat" with dots |
|||
</pre> |
|||
'''Preliminaries''' |
|||
<syntaxhighlight lang=jq> |
|||
### 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 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) ; |
|||
</syntaxhighlight> |
|||
'''Gaussian Primes''' |
|||
<syntaxhighlight lang=jq> |
|||
# emit a stream of Gaussian primes with real and imaginary parts within the given radius |
|||
# but use the square |real| < $Radius |
|||
def GPrimes($Radius): |
|||
($Radius | norm) as $R2 |
|||
| range(-$Radius; $Radius + 1) as $r |
|||
| range(-$Radius; $Radius + 1) as $i |
|||
| select( [$r,$i] | norm < $R2 ) |
|||
| if $i == 0 |
|||
then ($r|abs) as $m |
|||
| select( ($m|isPrime) and ($m - 3) % 4 == 0 ) | [$r,0] |
|||
elif $r == 0 |
|||
then ($i|abs) as $m |
|||
| select( $m|isPrime and ($m - 3) % 4 == 0 ) | [0, $i] |
|||
else ([$r, $i] | norm) as $n |
|||
| select( $n < ($Radius * $Radius) and ($n|isPrime)) | [$r, $i] |
|||
end ; |
|||
def plot($Radius): |
|||
"# X Y", |
|||
(GPrimes($Radius) | "\(first) \(last)"); |
|||
</syntaxhighlight> |
|||
'''Tasks''' |
|||
<syntaxhighlight lang=jq> |
|||
# 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) |
|||
</syntaxhighlight> |
|||
{{Output}} |
|||
Invocation: jq -nc -f gaussian-primes.jq |
|||
<pre> |
|||
Gaussian primes with norm < 100, grouped by the real part: |
|||
[-9,-4] [-9,4] |
|||
[-8,-5] [-8,-3] [-8,3] [-8,5] |
|||
[-7,-2] [-7,0] [-7,2] |
|||
[-6,-5] [-6,-1] [-6,1] [-6,5] |
|||
[-5,-8] [-5,-6] [-5,-4] [-5,-2] [-5,2] [-5,4] [-5,6] [-5,8] |
|||
[-4,-9] [-4,-5] [-4,-1] [-4,1] [-4,5] [-4,9] |
|||
[-3,-8] [-3,-2] [-3,0] [-3,2] [-3,8] |
|||
[-2,-7] [-2,-5] [-2,-3] [-2,-1] [-2,1] [-2,3] [-2,5] [-2,7] |
|||
[-1,-6] [-1,-4] [-1,-2] [-1,-1] [-1,1] [-1,2] [-1,4] [-1,6] |
|||
[0,-7] [0,-3] [0,3] [0,7] |
|||
[1,-6] [1,-4] [1,-2] [1,-1] [1,1] [1,2] [1,4] [1,6] |
|||
[2,-7] [2,-5] [2,-3] [2,-1] [2,1] [2,3] [2,5] [2,7] |
|||
[3,-8] [3,-2] [3,0] [3,2] [3,8] |
|||
[4,-9] [4,-5] [4,-1] [4,1] [4,5] [4,9] |
|||
[5,-8] [5,-6] [5,-4] [5,-2] [5,2] [5,4] [5,6] [5,8] |
|||
[6,-5] [6,-1] [6,1] [6,5] |
|||
[7,-2] [7,0] [7,2] |
|||
[8,-5] [8,-3] [8,3] [8,5] |
|||
[9,-4] [9,4] |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |