Prime Numbers
Overview
Prime numbers are the atomic building blocks of integer arithmetic, because every integer greater than 1 decomposes uniquely into primes up to ordering. In analysis and engineering, prime-focused computations support cryptography, hashing, randomization schemes, and large-scale integer diagnostics. This category centers on prime-number computation: testing primality, navigating the ordered prime sequence, counting primes in ranges, and extracting prime structure from integers. Together, these tools provide a practical bridge between textbook number theory and spreadsheet-ready workflows.
The unifying ideas are primality, unique factorization, and distribution of primes. Primality asks whether an integer has exactly two positive divisors; factorization resolves integers into prime powers; and counting/range queries summarize where primes occur. A useful asymptotic anchor is the prime number theorem, often written as \pi(n) \sim \frac{n}{\log n}, which explains why prime density decreases as values grow. These concepts connect the tools here: some return exact factors, some return positional or neighboring primes, and others return prime sets or counts over intervals.
Implementation is primarily backed by SymPy Number Theory (ntheory), with complementary utilities from primePy. SymPy provides mature routines for deterministic and probabilistic primality logic, sieve-based prime generation, and integer factorization algorithms (including trial division and Pollard-style methods). primePy contributes lightweight prime enumeration and primality helpers that are easy to integrate in small workflows. The category combines both ecosystems behind consistent Excel-style interfaces.
For prime decomposition workflows, FACTORINT and PRIMEFACTORS address different levels of detail. FACTORINT returns the full prime-power structure of an integer, including multiplicities, which is essential for divisor analysis, arithmetic-function evaluation, and reconstruction checks. PRIMEFACTORS returns only distinct prime divisors, which is often preferable for feature engineering, squarefree diagnostics, and quick signature-style comparisons across integers. In practice, these two functions are complementary: one gives complete exponents, the other gives the reduced prime support.
For primality decisions and sequence navigation, ISPRIME, PPY_PRIMECHECK, NEXTPRIME, PREVPRIME, and PRIME form a compact toolkit. ISPRIME and PPY_PRIMECHECK answer the yes/no primality question using different backends, which is useful for cross-validation and performance tradeoffs. NEXTPRIME and PREVPRIME move locally through the prime sequence around a boundary value, while PRIME performs direct index lookup of the nth prime. These operations are common in key-generation prototypes, search heuristics, and construction of prime-indexed parameter grids.
For interval generation, counting, and sampling, PRIMERANGE, PPY_PRIMEUPTO, PPY_PRIMEBETWEEN, PRIMEPI, and RANDPRIME support distribution-aware analysis. PRIMERANGE, PPY_PRIMEUPTO, and PPY_PRIMEBETWEEN enumerate primes over different boundary conventions, enabling reproducible candidate sets for benchmarking or simulation. PRIMEPI provides exact prime counts up to a threshold, a direct operational form of \pi(n) used in density tracking and scale comparisons. RANDPRIME adds stochastic selection from an interval, useful when experiments need prime-valued seeds or randomized test inputs constrained by primality.
FACTORINT
This function decomposes an integer into prime factors and their exponents.
For a nonzero integer n, the factorization is represented as:
n = \prod_{k=1}^{m} p_k^{e_k}
where each p_k is prime and each e_k is a positive integer. The output is a 2D table with one row per factor.
Excel Usage
=FACTORINT(n, limit, use_trial, use_rho, use_pm_one, use_ecm, verbose)
n(int, required): Integer to factor.limit(int, optional, default: null): Optional trial factorization limit.use_trial(bool, optional, default: true): Whether to use trial division.use_rho(bool, optional, default: true): Whether to use Pollard rho factor search.use_pm_one(bool, optional, default: true): Whether to use Pollard p minus 1 method.use_ecm(bool, optional, default: true): Whether to use elliptic curve method steps.verbose(bool, optional, default: false): Whether to print verbose factorization progress.
Returns (list[list]): Two-column 2D array of [prime_factor, exponent] rows.
Example 1: Factorization of sixty
Inputs:
| n | limit | use_trial | use_rho | use_pm_one | use_ecm | verbose |
|---|---|---|---|---|---|---|
| 60 | true | true | true | true | false |
Excel formula:
=FACTORINT(60, , TRUE, TRUE, TRUE, TRUE, FALSE)
Expected output:
| Result | |
|---|---|
| 2 | 2 |
| 3 | 1 |
| 5 | 1 |
Example 2: Factorization of a prime integer
Inputs:
| n | limit | use_trial | use_rho | use_pm_one | use_ecm | verbose |
|---|---|---|---|---|---|---|
| 97 | true | true | true | true | false |
Excel formula:
=FACTORINT(97, , TRUE, TRUE, TRUE, TRUE, FALSE)
Expected output:
| Result | |
|---|---|
| 97 | 1 |
Example 3: Factorization of one thousand two hundred sixty
Inputs:
| n | limit | use_trial | use_rho | use_pm_one | use_ecm | verbose |
|---|---|---|---|---|---|---|
| 1260 | true | true | true | true | false |
Excel formula:
=FACTORINT(1260, , TRUE, TRUE, TRUE, TRUE, FALSE)
Expected output:
| Result | |
|---|---|
| 2 | 2 |
| 3 | 2 |
| 5 | 1 |
| 7 | 1 |
Example 4: Factorization including sign for negative input
Inputs:
| n | limit | use_trial | use_rho | use_pm_one | use_ecm | verbose |
|---|---|---|---|---|---|---|
| -84 | true | true | true | true | false |
Excel formula:
=FACTORINT(-84, , TRUE, TRUE, TRUE, TRUE, FALSE)
Expected output:
| Result | |
|---|---|
| -1 | 1 |
| 2 | 2 |
| 3 | 1 |
| 7 | 1 |
Python Code
Show Code
from sympy import factorint as sympy_factorint
def factorint(n, limit=None, use_trial=True, use_rho=True, use_pm_one=True, use_ecm=True, verbose=False):
"""
Compute the prime factorization of an integer with multiplicities.
See: https://docs.sympy.org/latest/modules/ntheory.html
This example function is provided as-is without any representation of accuracy.
Args:
n (int): Integer to factor.
limit (int, optional): Optional trial factorization limit. Default is None.
use_trial (bool, optional): Whether to use trial division. Default is True.
use_rho (bool, optional): Whether to use Pollard rho factor search. Default is True.
use_pm_one (bool, optional): Whether to use Pollard p minus 1 method. Default is True.
use_ecm (bool, optional): Whether to use elliptic curve method steps. Default is True.
verbose (bool, optional): Whether to print verbose factorization progress. Default is False.
Returns:
list[list]: Two-column 2D array of [prime_factor, exponent] rows.
"""
try:
factors = sympy_factorint(
n,
limit=limit,
use_trial=use_trial,
use_rho=use_rho,
use_pm1=use_pm_one,
use_ecm=use_ecm,
verbose=verbose,
visual=False,
multiple=False,
)
rows = []
for factor, exponent in sorted(factors.items(), key=lambda item: int(item[0])):
rows.append([int(factor), int(exponent)])
return rows
except Exception as e:
return f"Error: {str(e)}"Online Calculator
ISPRIME
This function checks whether an integer is a prime number. A prime is a positive integer greater than 1 with exactly two positive divisors: 1 and itself.
The primality condition can be stated as:
n > 1 \text{ and } \nexists\ d \in \{2,\dots,\lfloor\sqrt{n}\rfloor\} \text{ such that } d \mid n
Negative numbers, 0, and 1 are not prime.
Excel Usage
=ISPRIME(n)
n(int, required): Integer to test for primality.
Returns (bool): True if the input is prime, otherwise False.
Example 1: Small prime number check
Inputs:
| n |
|---|
| 13 |
Excel formula:
=ISPRIME(13)
Expected output:
true
Example 2: Small composite number check
Inputs:
| n |
|---|
| 15 |
Excel formula:
=ISPRIME(15)
Expected output:
false
Example 3: One is not prime
Inputs:
| n |
|---|
| 1 |
Excel formula:
=ISPRIME(1)
Expected output:
false
Example 4: Negative integer is not prime
Inputs:
| n |
|---|
| -11 |
Excel formula:
=ISPRIME(-11)
Expected output:
false
Python Code
Show Code
from sympy import isprime as sympy_isprime
def isprime(n):
"""
Determine whether an integer is prime.
See: https://docs.sympy.org/latest/modules/ntheory.html
This example function is provided as-is without any representation of accuracy.
Args:
n (int): Integer to test for primality.
Returns:
bool: True if the input is prime, otherwise False.
"""
try:
return bool(sympy_isprime(n))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
NEXTPRIME
This function finds the next prime after a given integer, or more generally the i-th prime greater than that integer.
If p_k denotes the ordered sequence of primes, the function returns p_{m+i} where p_m \le n < p_{m+1}.
This is useful for stepping through the prime sequence from an arbitrary starting point.
Excel Usage
=NEXTPRIME(n, ith)
n(int, required): Starting integer.ith(int, optional, default: 1): Positive index offset in the sequence of primes.
Returns (int): The ith prime strictly greater than n.
Example 1: Next prime after ten
Inputs:
| n | ith |
|---|---|
| 10 | 1 |
Excel formula:
=NEXTPRIME(10, 1)
Expected output:
11
Example 2: Second prime after two
Inputs:
| n | ith |
|---|---|
| 2 | 2 |
Excel formula:
=NEXTPRIME(2, 2)
Expected output:
5
Example 3: Next prime after an existing prime
Inputs:
| n | ith |
|---|---|
| 13 | 1 |
Excel formula:
=NEXTPRIME(13, 1)
Expected output:
17
Example 4: Next prime after negative integer
Inputs:
| n | ith |
|---|---|
| -20 | 1 |
Excel formula:
=NEXTPRIME(-20, 1)
Expected output:
2
Python Code
Show Code
from sympy import nextprime as sympy_nextprime
def nextprime(n, ith=1):
"""
Return the ith prime number greater than a given integer.
See: https://docs.sympy.org/latest/modules/ntheory.html
This example function is provided as-is without any representation of accuracy.
Args:
n (int): Starting integer.
ith (int, optional): Positive index offset in the sequence of primes. Default is 1.
Returns:
int: The ith prime strictly greater than n.
"""
try:
result = sympy_nextprime(n, ith=ith)
return int(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
PPY_PRIMEBETWEEN
This function returns prime numbers within a bounded integer interval.
The output contains integers p that satisfy the interval conditions imposed by primePy’s between function and are prime.
Results are formatted as a single-column 2D array for Excel range compatibility.
Excel Usage
=PPY_PRIMEBETWEEN(m, n)
m(int, required): Lower bound of interval.n(int, required): Upper bound of interval.
Returns (list[list]): Single-column 2D array of prime numbers between m and n.
Example 1: Primes between ten and thirty
Inputs:
| m | n |
|---|---|
| 10 | 30 |
Excel formula:
=PPY_PRIMEBETWEEN(10, 30)
Expected output:
| Result |
|---|
| 11 |
| 13 |
| 17 |
| 19 |
| 23 |
| 29 |
Example 2: Primes between two and ten
Inputs:
| m | n |
|---|---|
| 2 | 10 |
Excel formula:
=PPY_PRIMEBETWEEN(2, 10)
Expected output:
| Result |
|---|
| 3 |
| 5 |
| 7 |
Example 3: Primes between fifty and eighty
Inputs:
| m | n |
|---|---|
| 50 | 80 |
Excel formula:
=PPY_PRIMEBETWEEN(50, 80)
Expected output:
| Result |
|---|
| 53 |
| 59 |
| 61 |
| 67 |
| 71 |
| 73 |
| 79 |
Example 4: Primes in a small interval window
Inputs:
| m | n |
|---|---|
| 90 | 100 |
Excel formula:
=PPY_PRIMEBETWEEN(90, 100)
Expected output:
97
Python Code
Show Code
from primePy import primes as primepy_primes
def ppy_primebetween(m, n):
"""
List prime numbers between two bounds using primePy.
See: https://github.com/janaindrajit/primePy
This example function is provided as-is without any representation of accuracy.
Args:
m (int): Lower bound of interval.
n (int): Upper bound of interval.
Returns:
list[list]: Single-column 2D array of prime numbers between m and n.
"""
try:
values = primepy_primes.between(m, n)
return [[int(v)] for v in values]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
PPY_PRIMECHECK
This function uses the primePy package to test whether an integer is prime.
A prime number is an integer greater than 1 that has exactly two positive divisors. The function returns a boolean indicating whether the input satisfies this property.
It provides a lightweight alternative implementation to other primality tests.
Excel Usage
=PPY_PRIMECHECK(num)
num(int, required): Integer to test for primality.
Returns (bool): True if the input is prime, otherwise False.
Example 1: primePy check for prime integer
Inputs:
| num |
|---|
| 17 |
Excel formula:
=PPY_PRIMECHECK(17)
Expected output:
true
Example 2: primePy check for composite integer
Inputs:
| num |
|---|
| 21 |
Excel formula:
=PPY_PRIMECHECK(21)
Expected output:
false
Example 3: primePy check for even non-prime
Inputs:
| num |
|---|
| 100 |
Excel formula:
=PPY_PRIMECHECK(100)
Expected output:
false
Example 4: primePy check for larger prime
Inputs:
| num |
|---|
| 997 |
Excel formula:
=PPY_PRIMECHECK(997)
Expected output:
true
Python Code
Show Code
from primePy import primes as primepy_primes
def ppy_primecheck(num):
"""
Check primality using the primePy implementation.
See: https://github.com/janaindrajit/primePy
This example function is provided as-is without any representation of accuracy.
Args:
num (int): Integer to test for primality.
Returns:
bool: True if the input is prime, otherwise False.
"""
try:
return bool(primepy_primes.check(num))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
PPY_PRIMEUPTO
This function returns all primes less than or equal to a maximum integer bound.
The result contains each integer p such that 2 \le p \le n and p is prime.
Values are returned as a single-column 2D array for Excel output.
Excel Usage
=PPY_PRIMEUPTO(n)
n(int, required): Inclusive upper bound for generated primes.
Returns (list[list]): Single-column 2D array of prime numbers up to n.
Example 1: Primes up to ten
Inputs:
| n |
|---|
| 10 |
Excel formula:
=PPY_PRIMEUPTO(10)
Expected output:
| Result |
|---|
| 2 |
| 3 |
| 5 |
| 7 |
Example 2: Primes up to thirty
Inputs:
| n |
|---|
| 30 |
Excel formula:
=PPY_PRIMEUPTO(30)
Expected output:
| Result |
|---|
| 2 |
| 3 |
| 5 |
| 7 |
| 11 |
| 13 |
| 17 |
| 19 |
| 23 |
| 29 |
Example 3: Primes up to two
Inputs:
| n |
|---|
| 2 |
Excel formula:
=PPY_PRIMEUPTO(2)
Expected output:
2
Example 4: Primes up to one hundred
Inputs:
| n |
|---|
| 100 |
Excel formula:
=PPY_PRIMEUPTO(100)
Expected output:
| Result |
|---|
| 2 |
| 3 |
| 5 |
| 7 |
| 11 |
| 13 |
| 17 |
| 19 |
| 23 |
| 29 |
| 31 |
| 37 |
| 41 |
| 43 |
| 47 |
| 53 |
| 59 |
| 61 |
| 67 |
| 71 |
| 73 |
| 79 |
| 83 |
| 89 |
| 97 |
Python Code
Show Code
from primePy import primes as primepy_primes
def ppy_primeupto(n):
"""
List all prime numbers up to a maximum value using primePy.
See: https://github.com/janaindrajit/primePy
This example function is provided as-is without any representation of accuracy.
Args:
n (int): Inclusive upper bound for generated primes.
Returns:
list[list]: Single-column 2D array of prime numbers up to n.
"""
try:
values = primepy_primes.upto(n)
return [[int(v)] for v in values]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
PREVPRIME
This function finds the greatest prime that is strictly less than the given integer.
For an integer n, it returns a prime p such that p < n and there is no prime q with p < q < n.
This operation is useful when traversing prime values in descending order.
Excel Usage
=PREVPRIME(n)
n(int, required): Upper bound integer.
Returns (int): Largest prime number strictly less than n.
Example 1: Previous prime below ten
Inputs:
| n |
|---|
| 10 |
Excel formula:
=PREVPRIME(10)
Expected output:
7
Example 2: Previous prime below thirteen
Inputs:
| n |
|---|
| 13 |
Excel formula:
=PREVPRIME(13)
Expected output:
11
Example 3: Previous prime below one hundred
Inputs:
| n |
|---|
| 100 |
Excel formula:
=PREVPRIME(100)
Expected output:
97
Example 4: Previous prime below three
Inputs:
| n |
|---|
| 3 |
Excel formula:
=PREVPRIME(3)
Expected output:
2
Python Code
Show Code
from sympy import prevprime as sympy_prevprime
def prevprime(n):
"""
Return the largest prime smaller than a given integer.
See: https://docs.sympy.org/latest/modules/ntheory.html
This example function is provided as-is without any representation of accuracy.
Args:
n (int): Upper bound integer.
Returns:
int: Largest prime number strictly less than n.
"""
try:
result = sympy_prevprime(n)
return int(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
PRIME
This function returns the prime at a given 1-based index in the ordered prime sequence.
If p_n denotes the nth prime, the function computes p_n where p_1 = 2, p_2 = 3, and so on.
This supports direct lookup of prime values by position.
Excel Usage
=PRIME(nth)
nth(int, required): One-based index of the desired prime number.
Returns (int): The nth prime number.
Example 1: First prime index
Inputs:
| nth |
|---|
| 1 |
Excel formula:
=PRIME(1)
Expected output:
2
Example 2: Tenth prime index
Inputs:
| nth |
|---|
| 10 |
Excel formula:
=PRIME(10)
Expected output:
29
Example 3: Fiftieth prime index
Inputs:
| nth |
|---|
| 50 |
Excel formula:
=PRIME(50)
Expected output:
229
Example 4: Hundredth prime index
Inputs:
| nth |
|---|
| 100 |
Excel formula:
=PRIME(100)
Expected output:
541
Python Code
Show Code
from sympy import prime as sympy_prime
def prime(nth):
"""
Return the nth prime number.
See: https://docs.sympy.org/latest/modules/ntheory.html
This example function is provided as-is without any representation of accuracy.
Args:
nth (int): One-based index of the desired prime number.
Returns:
int: The nth prime number.
"""
try:
result = sympy_prime(nth)
return int(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
PRIMEFACTORS
This function returns the set of prime divisors of an integer, sorted in ascending order and without multiplicities.
If the full prime-power factorization is n = \prod p_k^{e_k}, this function returns only the distinct primes \{p_k\}.
The output is provided as a single-column 2D array for Excel compatibility.
Excel Usage
=PRIMEFACTORS(n, limit, verbose)
n(int, required): Integer whose prime divisors are requested.limit(int, optional, default: null): Optional factorization limit for partial search.verbose(bool, optional, default: false): Whether to show detailed factorization progress.
Returns (list[list]): Single-column 2D array of distinct prime factors.
Example 1: Distinct prime factors of six
Inputs:
| n | limit | verbose |
|---|---|---|
| 6 | false |
Excel formula:
=PRIMEFACTORS(6, , FALSE)
Expected output:
| Result |
|---|
| 2 |
| 3 |
Example 2: Distinct prime factors of one hundred twenty three thousand four hundred fifty six
Inputs:
| n | limit | verbose |
|---|---|---|
| 123456 | false |
Excel formula:
=PRIMEFACTORS(123456, , FALSE)
Expected output:
| Result |
|---|
| 2 |
| 3 |
| 643 |
Example 3: Distinct prime factors of negative number
Inputs:
| n | limit | verbose |
|---|---|---|
| -84 | false |
Excel formula:
=PRIMEFACTORS(-84, , FALSE)
Expected output:
| Result |
|---|
| 2 |
| 3 |
| 7 |
Example 4: Distinct prime factors of a prime input
Inputs:
| n | limit | verbose |
|---|---|---|
| 101 | false |
Excel formula:
=PRIMEFACTORS(101, , FALSE)
Expected output:
101
Python Code
Show Code
from sympy import primefactors as sympy_primefactors
def primefactors(n, limit=None, verbose=False):
"""
Return distinct prime factors of an integer.
See: https://docs.sympy.org/latest/modules/ntheory.html
This example function is provided as-is without any representation of accuracy.
Args:
n (int): Integer whose prime divisors are requested.
limit (int, optional): Optional factorization limit for partial search. Default is None.
verbose (bool, optional): Whether to show detailed factorization progress. Default is False.
Returns:
list[list]: Single-column 2D array of distinct prime factors.
"""
try:
values = sympy_primefactors(n, limit=limit, verbose=verbose)
return [[int(v)] for v in values]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
PRIMEPI
This function evaluates the prime-counting function \pi(n), which gives how many prime numbers are less than or equal to n.
Formally:
\pi(n) = \left|\{p \le n : p \text{ is prime}\}\right|
It is useful for density and distribution analysis of prime numbers.
Excel Usage
=PRIMEPI(n)
n(int, required): Inclusive upper limit for counting primes.
Returns (int): Number of primes less than or equal to n.
Example 1: Prime count up to ten
Inputs:
| n |
|---|
| 10 |
Excel formula:
=PRIMEPI(10)
Expected output:
4
Example 2: Prime count up to twenty five
Inputs:
| n |
|---|
| 25 |
Excel formula:
=PRIMEPI(25)
Expected output:
9
Example 3: Prime count up to one hundred
Inputs:
| n |
|---|
| 100 |
Excel formula:
=PRIMEPI(100)
Expected output:
25
Example 4: Prime count up to one
Inputs:
| n |
|---|
| 1 |
Excel formula:
=PRIMEPI(1)
Expected output:
0
Python Code
Show Code
from sympy import primepi as sympy_primepi
def primepi(n):
"""
Count the number of primes less than or equal to an integer.
See: https://docs.sympy.org/latest/modules/ntheory.html
This example function is provided as-is without any representation of accuracy.
Args:
n (int): Inclusive upper limit for counting primes.
Returns:
int: Number of primes less than or equal to n.
"""
try:
result = sympy_primepi(n)
return int(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
PRIMERANGE
This function returns all prime numbers in a half-open interval. With one bound, it returns primes in [2, a); with two bounds, it returns primes in [a, b).
The output is the ordered set of integers p such that each p is prime and satisfies the interval constraints.
The returned values are arranged as a single-column 2D array for Excel range output.
Excel Usage
=PRIMERANGE(a, b)
a(int, required): Lower bound or upper bound when b is omitted.b(int, optional, default: null): Exclusive upper bound when provided.
Returns (list[list]): Single-column 2D array of prime numbers in the requested range.
Example 1: Primes less than twenty
Inputs:
| a | b |
|---|---|
| 20 |
Excel formula:
=PRIMERANGE(20, )
Expected output:
| Result |
|---|
| 2 |
| 3 |
| 5 |
| 7 |
| 11 |
| 13 |
| 17 |
| 19 |
Example 2: Primes in a bounded interval
Inputs:
| a | b |
|---|---|
| 7 | 30 |
Excel formula:
=PRIMERANGE(7, 30)
Expected output:
| Result |
|---|
| 7 |
| 11 |
| 13 |
| 17 |
| 19 |
| 23 |
| 29 |
Example 3: Interval with few primes
Inputs:
| a | b |
|---|---|
| 30 | 40 |
Excel formula:
=PRIMERANGE(30, 40)
Expected output:
| Result |
|---|
| 31 |
| 37 |
Example 4: Small upper bound interval
Inputs:
| a | b |
|---|---|
| 5 |
Excel formula:
=PRIMERANGE(5, )
Expected output:
| Result |
|---|
| 2 |
| 3 |
Python Code
Show Code
from sympy import primerange as sympy_primerange
def primerange(a, b=None):
"""
Generate all prime numbers in a specified interval.
See: https://docs.sympy.org/latest/modules/ntheory.html
This example function is provided as-is without any representation of accuracy.
Args:
a (int): Lower bound or upper bound when b is omitted.
b (int, optional): Exclusive upper bound when provided. Default is None.
Returns:
list[list]: Single-column 2D array of prime numbers in the requested range.
"""
try:
if b is None:
values = list(sympy_primerange(a))
else:
values = list(sympy_primerange(a, b))
return [[int(v)] for v in values]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
RANDPRIME
This function returns a prime selected from the interval [a, b).
The sampled value is constrained by primality and interval bounds:
p \in [a, b), \quad p \text{ is prime}
A fixed random seed is used to make results reproducible across test runs.
Excel Usage
=RANDPRIME(a, b)
a(int, required): Inclusive lower bound.b(int, required): Exclusive upper bound.
Returns (int): A prime number sampled from the requested range.
Example 1: Sample prime from one to thirty
Inputs:
| a | b |
|---|---|
| 1 | 30 |
Excel formula:
=RANDPRIME(1, 30)
Expected output:
11
Example 2: Sample prime from fifty to one hundred
Inputs:
| a | b |
|---|---|
| 50 | 100 |
Excel formula:
=RANDPRIME(50, 100)
Expected output:
89
Example 3: Sample prime from one hundred to one twenty
Inputs:
| a | b |
|---|---|
| 100 | 120 |
Excel formula:
=RANDPRIME(100, 120)
Expected output:
113
Example 4: Sample prime using Bertrand-style interval
Inputs:
| a | b |
|---|---|
| 30 | 60 |
Excel formula:
=RANDPRIME(30, 60)
Expected output:
41
Python Code
Show Code
import random
from sympy import randprime as sympy_randprime
def randprime(a, b):
"""
Sample a prime number from a half-open integer interval.
See: https://docs.sympy.org/latest/modules/ntheory.html
This example function is provided as-is without any representation of accuracy.
Args:
a (int): Inclusive lower bound.
b (int): Exclusive upper bound.
Returns:
int: A prime number sampled from the requested range.
"""
try:
random.seed(0)
result = sympy_randprime(a, b)
return int(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator