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

Published on 12 May 2024 09:40 PM

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

The is_gaussian_prime function checks if a given complex number is a Gaussian prime. A Gaussian prime is a non-unit Gaussian integer (a complex number of the form a + bi, where a and b are integers) that is divisible only by itself and by the units 1, i, -1, and -i.

The function first checks if the real or imaginary part of the complex number is zero. If either part is zero, then the complex number is a Gaussian prime if and only if the absolute value of the non-zero part is a real prime number and is congruent to 3 modulo 4.

If both the real and imaginary parts of the complex number are non-zero, then the function checks if the norm of the complex number (the square of its absolute value) is a real prime number. If the norm is a real prime number, then the complex number is a Gaussian prime.

The main function __main__ generates a list of complex numbers within a given limit (100 in this case) and then filters the list to only include Gaussian primes. The Gaussian primes are then sorted by their norm and printed to the console.

The matplotlib.pyplot library is used to plot the Gaussian primes on the complex plane. The scatter function is used to plot the real and imaginary parts of the Gaussian primes as points on the plane.

Source code in the python programming language

''' python example for task rosettacode.org/wiki/Gaussian_primes '''

from matplotlib.pyplot import scatter
from sympy import isprime
from math import isqrt

def norm(c):
    ''' Task complex norm function '''
    return c.real * c.real + c.imag * c.imag


def is_gaussian_prime(n):
    '''
        is_gaussian_prime(n)
     
    A Gaussian prime is a non-unit Gaussian integer m + ni divisible only by its associates and by the units
    1, i, -1, -i and by no other Gaussian integers.
     
    The Gaussian primes fall into one of three categories:
     
    Gaussian integers with imaginary part zero and a prime real part m with |m| a real prime satisfying |m| = 3 mod 4
    Gaussian integers with real part zero and an imaginary part n with |n| real prime satisfying  |n| = 3 mod 4
    Gaussian integers having both real and imaginary parts, and its complex norm (square of algebraic norm) is a real prime number
    '''
    r, c = int(abs(n.real)), int(abs(n.imag))
    return isprime(r * r + c * c) or c == 0 and isprime(r) and (r - 3) % 4 == 0 or r == 0 and isprime(c) and (c - 3) % 4 == 0

if __name__ == '__main__':

    limitsquared = 100
    lim = isqrt(limitsquared)
    testvals = [complex(r, c) for r in range(-lim, lim) for c in range(-lim, lim)]
    gprimes = sorted(filter(lambda c : is_gaussian_prime(c) and norm(c) < limitsquared, testvals), key=norm)
    print(f'Gaussian primes within {isqrt(limitsquared)} of the origin on the complex plane:')
    for i, c in enumerate(gprimes):
        print(str(c).ljust(9), end='\n' if (i +1) % 10 == 0 else '')
    scatter([c.real for c in gprimes], [c.imag for c in gprimes])


  

You may also check:How to resolve the algorithm Active Directory/Connect step by step in the C# programming language
You may also check:How to resolve the algorithm Gotchas step by step in the Quackery programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Rust programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the 8th programming language
You may also check:How to resolve the algorithm Idiomatically determine all the lowercase and uppercase letters step by step in the COBOL programming language