isprime

Tests which elements are prime numbers.

Category: Mathematics > Elementary Math

Syntax

TF = isprime(X)

Returns a logical array that is true where the element of X is prime.

Description

A prime is an integer greater than 1 whose only positive divisors are 1 and itself. So 0 and 1 are not prime, and 2 is the only even prime.

Combine with find to list primes in a range, or with sum to count them: sum(isprime(1:100)) is 25.

Input arguments

X
Array of nonnegative integer values (any numeric class).

Output arguments

TF
Logical array of the same size as X.

Example

Primes up to 10 and a Mersenne prime

isprime(1:10)
isprime(2^31 - 1)
sum(isprime(1:100))
Output
ans = 1×10 logical array

   0   1   1   0   1   0   1   0   0   0
ans = logical
   1
ans = 25

See also

primes factor gcd nchoosek