Navigation

Operators and Keywords

Function List:

C++ API

: isprime (x)

Return a logical array which is true where the elements of x are prime numbers and false where they are not.

A prime number is conventionally defined as a positive integer greater than 1 (e.g., 2, 3, …) which is divisible only by itself and 1. Octave extends this definition to include both negative integers and complex values. A negative integer is prime if its positive counterpart is prime. This is equivalent to isprime (abs (x)).

If class (x) is complex, then primality is tested in the domain of Gaussian integers (http://en.wikipedia.org/wiki/Gaussian_integer). Some non-complex integers are prime in the ordinary sense, but not in the domain of Gaussian integers. For example, 5 = (1+2i)*(1-2i) shows that 5 is not prime because it has a factor other than itself and 1. Exercise caution when testing complex and real values together in the same matrix.

Examples:

isprime (1:6)
    ⇒ [0, 1, 1, 0, 1, 0]
isprime ([i, 2, 3, 5])
    ⇒ [0, 0, 1, 0]

Programming Note: isprime is appropriate if the maximum value in x is not too large (< 1e15). For larger values special purpose factorization code should be used.

Compatibility Note: matlab does not extend the definition of prime numbers and will produce an error if given negative or complex inputs.

See also: primes, factor, gcd, lcm.

Package: octave