Gaussian primes

func gaussianprimes(size) {
    var plot = (2*size + 1 -> of { 2*size + 1 -> of(' ') })
    var primes = []
    for A in (-size .. size) {
        var limit = isqrt(size**2 - A**2)
        for B in (-limit .. limit) {
            var n = Gauss(A, B)
            if (n.is_prime) {
                primes << n.to_n
                plot[B + size + 1][A + size + 1] = 'X'
            }
        }
    }
    return (plot, primes)
}

with(10) {|n|
    var(_plot, primes) = gaussianprimes(n)
    say "Primes within #{n}:"
    primes.slices(10).each { .map{'%7s'%_}.join(", ").say }
}

with (50) {|n|
    var (plot) = gaussianprimes(n)
    say "\nPlot within #{n}"
    plot.each { .join('').say }
}

Output:

The plot up to 50 is identical to the one produced by the Perl entry.

Last updated

Was this helpful?