Main Content

nextprime

Next prime number

Description

example

nextprime(n) returns the next prime number greater than or equal to n. If n is a vector or matrix, then nextprime acts element-wise on n.

Examples

Find Next Prime Number

Find the next prime number greater than 100.

nextprime(100)
ans =
101

Find the next prime numbers greater than 1000, 10000, and 100000 by specifying the input as a vector.

v = [1000 10000 100000];
nextprime(v)
ans =
        1009       10007      100003

Find Large Prime Number

When finding large prime numbers, return exact symbolic integers by using symbolic input. Further, if your input has 15 or more digits, then use quotation marks and wrap the number in sym to represent the number accurately. For more information, see Numeric to Symbolic Conversion.

Find a large prime number by using 10^sym(18).

nextprime(10^sym(18))
ans =
1000000000000000003

Find the next prime number to 823572345728582545 by using quotation marks.

nextprime(sym('823572345728582545'))
ans =
823572345728582623

Input Arguments

collapse all

Input, specified as a number, vector, matrix, array, or a symbolic number or array.

Version History

Introduced in R2016b