Main Content

# prevprime

Previous prime number

## Syntax

``prevprime(n)``

## Description

example

````prevprime(n)` returns the largest prime number smaller than or equal to `n`. If `n` is a vector or matrix, then `prevprime` acts element-wise on `n`.```

## Examples

### Find Previous Prime Number

Find the largest prime number smaller than `100`.

`prevprime(100)`
```ans = 97```

Find the largest prime numbers smaller than `1000`, `10000`, and `100000` by specifying the input as a vector.

```v = [1000 10000 100000]; prevprime(v)```
```ans = 997 9973 99991```

### 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)`.

`prevprime(10^sym(18))`
```ans = 999999999999999989```

Find the prime number previous to `823572345728582545` by using quotation marks.

`prevprime(sym('823572345728582545'))`
```ans = 823572345728582543```

## Input Arguments

collapse all

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

## See Also

Introduced in R2016b

## Support

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos