prime_list

prime_list

Utilities for prime number generation.

This module provides a function for generating all prime numbers up to and including a given upper bound using the Sieve of Eratosthenes, with attention to algorithmic efficiency and edge case handling.

Functions

Name Description
prime_list >> Generate a list of all prime numbers up to and including n.

prime_list

prime_list.prime_list(n)

Generate a list of all prime numbers up to and including n.

This function uses the Sieve of Eratosthenes algorithm to efficiently find all prime numbers in the range [2, n].

Parameters

Name Type Description Default
n int The upper bound (inclusive) for finding prime numbers. Must be a non-negative integer. required

Returns

Name Type Description
list of int A list containing all prime numbers from 2 up to and including n, in ascending order. Returns an empty list if n < 2.

Raises

Name Type Description
Exception If n is not an integer.
Exception If n is a negative integer.

Examples

>>> prime_list(10)
[2, 3, 5, 7]
>>> prime_list(20)
[2, 3, 5, 7, 11, 13, 17, 19]
>>> prime_list(2)
[2]
>>> prime_list(1)
[]
>>> prime_list(0)
[]

Notes

The Sieve of Eratosthenes works by iteratively marking the multiples of each prime number starting from 2. The algorithm has O(n log log n) time complexity and O(n) space complexity, making it efficient for finding all primes up to a given limit.

This implementation is optimized by only checking multiples starting from the square of each prime, and only iterating up to the square root of n for the sieving process.