isprime

Primality test

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Syntax

isprime(n)

Description

isprime(n) checks whether n is a prime number.

isprime is a fast probabilistic prime number test (Miller-Rabin test). The function returns TRUE when the positive integern is either a prime number or a strong pseudo-prime for 10 independently and randomly chosen bases. Otherwise, isprime returns FALSE.

If n is positive and isprime returns FALSE, then n is guaranteed to be composite. If n is positive and isprime returns TRUE, then n is prime with a very high probability.

Use numlib::proveprime for a prime number test that always returns the correct answer. Note, however, that it is usually much slower than isprime.

isprime() and isprime(1) return FALSE. isprime returns always FALSE if n is a negative integer.

isprime returns an error message if its argument is a number but not an integer. isprime returns a symbolic isprime call if the argument is not a number.

Examples

Example 1

The number 989999 is prime:

isprime(989999)

ifactor(989999)

In contrast to ifactor, isprime can handle large numbers:

isprime(2^(2^11) + 1)

isprime() and isprime(1) return FALSE:

isprime(0), isprime(1)

Negative numbers yield FALSE as well:

isprime(-13)

For non-numeric arguments, a symbolic isprime call is returned:

delete n: isprime(n)

Parameters

n

An arithmetical expression representing an integer

Return Values

Either TRUE or FALSE, or a symbolic isprime call.

Algorithms

Reference: Michael O. Rabin, Probabilistic algorithms, in J. F. Traub, ed., Algorithms and Complexity, Academic Press, New York, 1976, pp. 21–39.