Gaussian primes: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 339: | Line 339: | ||
Off-site SVG image: [https://raw.githubusercontent.com/thundergnat/rc/master/img/gaussian-primes-raku.svg gaussian-primes-raku.svg] |
Off-site SVG image: [https://raw.githubusercontent.com/thundergnat/rc/master/img/gaussian-primes-raku.svg gaussian-primes-raku.svg] |
||
=={{header|Wren}}== |
|||
{{libheader|DOME}} |
|||
{{libheader|Wren-plot}} |
|||
{{libheader|Wren-complex}} |
|||
{{libheader|Wren-math}} |
|||
{{libheader|wren-fmt}} |
|||
Plots the points up to a radius of 150 to produce a similar image to the Raku example. |
|||
<lang ecmascript>import "dome" for Window |
|||
import "graphics" for Canvas, Color |
|||
import "./plot" for Axes |
|||
import "./complex" for Complex |
|||
import "./math2" for Int |
|||
import "./fmt" for Fmt |
|||
var norm = Fn.new { |c| c.real * c.real + c.imag * c.imag } |
|||
var GPrimes = [] |
|||
var Radius = 150 |
|||
for (r in -Radius+1...Radius) { |
|||
for (i in -Radius+1...Radius) { |
|||
if (i == 0) { |
|||
var m = r.abs |
|||
if (Int.isPrime(m) && (m - 3) % 4 == 0) GPrimes.add(Complex.new(r)) |
|||
} else if (r == 0) { |
|||
var m = i.abs |
|||
if (Int.isPrime(m) && (m - 3) % 4 == 0) GPrimes.add(Complex.new(0, i)) |
|||
} else { |
|||
var n = r * r + i * i |
|||
if (n < Radius * Radius && Int.isPrime(n)) GPrimes.add(Complex.new(r, i)) |
|||
} |
|||
} |
|||
} |
|||
var gp10 = GPrimes.where { |p| norm.call(p) < 100 }.toList |
|||
gp10.sort { |i, j| |
|||
var ni = norm.call(i) |
|||
var nj = norm.call(j) |
|||
if (ni != nj) return ni < nj |
|||
if (i.real != j.real) return i.real > j.real |
|||
return i.imag > j.imag |
|||
} |
|||
System.print("Gaussian primes with a norm less than 100 sorted by norm:") |
|||
Fmt.tprint("($2.0z) ", gp10, 5) |
|||
GPrimes = GPrimes.map { |c| c.toPair }.toList |
|||
class Main { |
|||
construct new() { |
|||
Window.title = "Gaussian primes" |
|||
Canvas.resize(1000, 1000) |
|||
Window.resize(1000, 1000) |
|||
Canvas.cls(Color.black) |
|||
var axes = Axes.new(100, 900, 800, 800, -Radius..Radius, -Radius..Radius) |
|||
axes.plot(GPrimes, Color.yellow, "·") |
|||
} |
|||
init() {} |
|||
update() {} |
|||
draw(alpha) {} |
|||
} |
|||
var Game = Main.new()</lang> |
|||
{{out}} |
|||
Terminal output: |
|||
<pre> |
|||
Gaussian primes with a norm less than 100 sorted by norm: |
|||
( 1 + 1i) ( 1 - 1i) (-1 + 1i) (-1 - 1i) ( 2 + 1i) |
|||
( 2 - 1i) ( 1 + 2i) ( 1 - 2i) (-1 + 2i) (-1 - 2i) |
|||
(-2 + 1i) (-2 - 1i) ( 3 + 0i) ( 0 + 3i) ( 0 - 3i) |
|||
(-3 + 0i) ( 3 + 2i) ( 3 - 2i) ( 2 + 3i) ( 2 - 3i) |
|||
(-2 + 3i) (-2 - 3i) (-3 + 2i) (-3 - 2i) ( 4 + 1i) |
|||
( 4 - 1i) ( 1 + 4i) ( 1 - 4i) (-1 + 4i) (-1 - 4i) |
|||
(-4 + 1i) (-4 - 1i) ( 5 + 2i) ( 5 - 2i) ( 2 + 5i) |
|||
( 2 - 5i) (-2 + 5i) (-2 - 5i) (-5 + 2i) (-5 - 2i) |
|||
( 6 + 1i) ( 6 - 1i) ( 1 + 6i) ( 1 - 6i) (-1 + 6i) |
|||
(-1 - 6i) (-6 + 1i) (-6 - 1i) ( 5 + 4i) ( 5 - 4i) |
|||
( 4 + 5i) ( 4 - 5i) (-4 + 5i) (-4 - 5i) (-5 + 4i) |
|||
(-5 - 4i) ( 7 + 0i) ( 0 + 7i) ( 0 - 7i) (-7 + 0i) |
|||
( 7 + 2i) ( 7 - 2i) ( 2 + 7i) ( 2 - 7i) (-2 + 7i) |
|||
(-2 - 7i) (-7 + 2i) (-7 - 2i) ( 6 + 5i) ( 6 - 5i) |
|||
( 5 + 6i) ( 5 - 6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