How to resolve the algorithm Gaussian primes step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Gaussian primes step by step in the Raku 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 Raku programming language

Source code in the raku programming language

use List::Divvy;

my @next = { :1x, :1y, :2n },;

sub next-interval (Int $int) {
     @next.append: (^$int).map: { %( :x($int), :y($_), :n($int² + .²) ) };
     @next = |@next.sort: *.;
}

my @gaussian = lazy gather {
    my $interval = 1;
    loop {
        my @this = @next.shift;
        @this.push: @next.shift while @next and @next[0] == @this[0];
        for @this {
            .take if ..is-prime || (!. && ..is-prime && (. - 3) %% 4);
            next-interval(++$interval) if $interval == .
        }
    }
}

# Primes within a radius of 10 from origin
say "Gaussian primes with a norm less than 100 sorted by norm:";
say @gaussian.&before(*. > 10²).map( {
     my (\i, \j) = .;
    flat ((i,j),(-i,j),(-i,-j),(i,-j),(j,i),(-j,i),(-j,-i),(j,-i)).map: {
        .[0] ?? .[1] ?? (sprintf "%d%s%di", .[0], (.[1] ≥ 0 ?? '+' !! ''), .[1]) !! .[0] !! "{.[1]}i"
    }} )».subst('1i', 'i', :g)».fmt("%6s")».unique.flat.batch(10).join: "\n" ;


# Plot points within a 150 radius
use SVG;

my @points = unique flat @gaussian.&before(*. > 150²).map: {
    my (\i, \j) = .;
    do for (i,j),(-i,j),(-i,-j),(i,-j),(j,i),(-j,i),(-j,-i),(j,-i) {
        :use['xlink:href'=>'#point', 'transform'=>"translate({500 + 3 × .[0]},{500 + 3 × .[1]})"]
    }
}

'gaussian-primes-raku.svg'.IO.spurt: SVG.serialize(
    svg => [
        :width<1000>, :height<1000>,
        :rect[:width<100%>, :height<100%>, :style],
        :defs[:g[:id, :circle[:0cx, :0cy, :2r, :fill('gold')]]],
        |@points
    ],
);


  

You may also check:How to resolve the algorithm Perfect totient numbers step by step in the bc programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the OCaml programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Maple programming language
You may also check:How to resolve the algorithm Pythagorean quadruples step by step in the J programming language
You may also check:How to resolve the algorithm Comments step by step in the Eiffel programming language