nextprime

Smallest prime greater than or equal to n.

Category: Mathematics > Number Theory

Syntax

p = nextprime(n)

Returns the smallest prime p >= n.

Description

If n is itself prime, it is returned unchanged; otherwise the function walks up to the next prime. Any n below 2 gives 2.

Use it to pick a prime table size, a hash modulus, or a test modulus just above a given bound.

Input arguments

n
Integer scalar or array, double or symbolic.

Output arguments

p
Primes with the size of n.

Example

Next primes

nextprime(14)
nextprime(97)
nextprime([10 20 1000000])
Output
ans = 17
ans = 97
ans = 1×3

          11          23     1000003

See also

prevprime nthprime isprime primes