Main Content

nthprime

nth prime number

Description

nthprime(n) returns the nth prime number. nthprime acts element-wise on array inputs.

example

Examples

collapse all

Find the 223rd prime number.

nthprime(223)
ans =
        1409

For large prime numbers, return exact symbolic integers by using symbolic input.

n = sym(223222222);
nthprime(n)
ans =
4738278383

Find the 10th, 100th, and 1000th prime numbers.

n = [10 100 1000];
nthprime(n)
ans =
          29         541        7919

Generate a random prime number between the 100,000th and 200,000th prime numbers.

rng default % for reproducibility
range = [100000 200000];
nthprime(randi(range))
ans =
     2476423

Input Arguments

collapse all

Input, specified as a number, vector, matrix, array, or a symbolic number or array. n must be a positive integer.

Version History

Introduced in R2018a