num_theory
Submodules
Attributes
Functions
|
Generate terms of an arithmetic progression (AP), compute the nth term, or calculate the sum of the first n terms. |
|
Check if a number is prime. |
Compute the prime factorization of a given integer. |
|
|
Returns the list of all prime numbers less than or equal to n. |
Package Contents
- num_theory.__version__
- num_theory.arithmetic_progression(a, d, n, compute_sum=False, nth_term=False)[source]
Generate terms of an arithmetic progression (AP), compute the nth term, or calculate the sum of the first n terms.
- Parameters:
a (float) – The first term of the AP.
d (float) – The common difference between consecutive terms.
n (int) – The number of terms, the term to compute, or the term index.
compute_sum (bool, optional) – If True, computes the sum of the first n terms. Default is False.
nth_term (bool, optional) – If True, computes the nth term of the AP instead of generating terms. Default is False.
- Returns:
If compute_sum and nth_term are both False, returns a list of the first n terms of the AP.
If nth_term is True, returns the nth term as a float.
If compute_sum is True, returns the sum of the first n terms as a float.
If both nth_term and compute_sum is True, it will return the nth term as a float.
- Return type:
list or float
Examples
>>> arithmetic_progression(a=2, d=3, n=5) [2, 5, 8, 11, 14]
>>> arithmetic_progression(a=2, d=3, n=5, compute_sum=True) 40.0
>>> arithmetic_progression(a=2, d=3, n=5, nth_term=True) 14
>>> arithmetic_progression(a=1, d=2, n=5, nth_term=True) 9
- num_theory.is_prime(n)[source]
Check if a number is prime.
- Parameters:
n (int) – The number to check for primality. Must be a positive integer greater than 1.
- Returns:
Returns True if the number is prime, otherwise False.
- Return type:
bool
- Raises:
ValueError – If the input is not a positive integer greater than 1.
Examples
>>> is_prime(2) True >>> is_prime(4) False >>> is_prime(13) True
- num_theory.prime_factorization(n)[source]
Compute the prime factorization of a given integer.
- Parameters:
n (int) – The integer to factorize. Must be a positive integer greater than 1.
- Returns:
A list of tuples where each tuple represents a prime factor and its corresponding power. Example: For n=28, the output will be [(2, 2), (7, 1)], indicating 28 = 2^2 * 7^1.
- Return type:
list of tuple
- Raises:
ValueError – If the input n is not a positive integer greater than 1.
Examples
>>> prime_factorization(28) [(2, 2), (7, 1)]
>>> prime_factorization(100) [(2, 2), (5, 2)]
Notes
This function uses trial division to determine the prime factors.
For large values of n, consider optimized algorithms such as Pollard’s Rho or the Quadratic Sieve for better performance.