This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Primality test

MuPAD® notebooks are not recommended. 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.




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.


Example 1

The number 989999 is prime:



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:


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

delete n: isprime(n)



An arithmetical expression representing an integer

Return Values

Either TRUE or FALSE, or a symbolic isprime call.


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

Was this topic helpful?