How to resolve the algorithm Gaussian primes step by step in the Nim programming language
How to resolve the algorithm Gaussian primes step by step in the Nim 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 Nim programming language
Source code in the nim programming language
import std/[algorithm, math, strformat]
import gnuplot
type IntComplex = tuple[re, im: int]
func isPrime(n: Natural): bool =
if n < 2: return false
if (n and 1) == 0: return n == 2
var d = 3
while d * d <= n:
if n mod d == 0:
return false
inc d, 2
result = true
func norm(c: IntComplex): Natural =
c.re * c.re + c.im * c.im
func `$`(c: IntComplex): string =
if c.im == 0: return $c.re
if c.re == 0: return $c.im & 'i'
let op = if c.im > 0: '+' else: '-'
result = &"{c.re}{op}{abs(c.im)}i"
func isGaussianPrime(c: IntComplex): bool =
if c.re == 0:
let x = abs(c.im)
return x.isPrime and (x and 3) == 3
if c.im == 0:
let x = abs(c.re)
return x.isPrime and (x and 3) == 3
result = c.norm.isPrime
func gaussianPrimes(maxNorm: Positive): seq[IntComplex] =
var gpList: seq[IntComplex]
let m = sqrt(maxNorm.toFloat).int
for x in -m..m:
for y in -m..m:
let c = (x, y)
if c.norm < maxNorm and c.isGaussianPrime:
gpList.add c
result = gpList.sortedByIt(it.norm)
echo "Gaussian primes with a norm less than 100 sorted by norm:"
for i, gp in gaussianPrimes(100):
stdout.write &"{gp:>5}"
stdout.write if i mod 10 == 9: '\n' else: ' '
var x, y: seq[int]
for gp in gaussianPrimes(150^2):
x.add gp.re
y.add gp.im
withGnuPlot:
cmd "set size ratio -1"
plot(x, y, "Gaussian primes", "with dots lw 2")
You may also check:How to resolve the algorithm Input loop step by step in the Ring programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the UnixPipes programming language
You may also check:How to resolve the algorithm De Polignac numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm File size step by step in the COBOL programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the Racket programming language