prevprime

Largest prime less than or equal to n.

Category: Mathematics > Number Theory

Syntax

p = prevprime(n)

Returns the largest prime p <= n.

Description

If n is prime it is returned unchanged; otherwise the function walks down to the nearest smaller prime. There is no prime below 2, so prevprime(1) is an error.

Input arguments

n
Integer scalar or array, double or symbolic, at least 2.

Output arguments

p
Primes with the size of n.

Example

Previous primes

prevprime(100)
prevprime(97)
prevprime(sym(1000))
Output
ans = 97
ans = 97
ans = 997

See also

nextprime nthprime isprime primes