Main Content

iscoprime

Check coprime relation

Since R2021a

Description

iscp = iscoprime(x) returns iscp as true if all the elements of the array x are coprime and returns iscp as false if two or more elements of x have a greatest common divisor (gcd) greater than 1.

example

[iscp,ispcp] = iscoprime(x) also returns ispcp as true to indicate if any of the elements in x are pairwise coprime.

[iscp,ispcp,pidx] = iscoprime(x) also returns the indices of all pairs of elements of x. pidx is a 2-row matrix whose number of columns equals to M choose 2. Each column of pidx specifies the indices of a pair of elements in x.

example

[iscp,ispcp,pidx,pgcd] = iscoprime(x) checks if pairs of elements of x have a greatest common divisor greater than 1. This syntax also returns the indices of all pairs of elements of x and the greatest common divisor of each pair. Each element in pgcd is the greatest common divisor of the two elements in x identified by the indices in the corresponding column of pidx.

Examples

collapse all

Create an array x whose elements are 9=3×3, 15=3×5, and 25=5×5. Verify that all elements of x are coprime.

x = [9 15 25];

Verify that at least one pair of elements of x has a greatest common divisor greater than 1. Output the pairs and their greatest common divisors.

[iscp,ispcp,pidx,pgcd] = iscoprime(x)
iscp = logical
   1

ispcp = logical
   0

pidx = 2×3

     1     1     2
     2     3     3

pgcd = 1×3

     3     1     5

Create an array of integers; 9=3×3, 15=3×5, and 25=5×5.

x = [9 15 25];

Show that the integers form a coprime set.

iscp = iscoprime(x)
iscp = logical
   1

Verify that at least one of the three possible pairs of integers form a coprime pair.

[icsp,ispcp] = iscoprime(x)
icsp = logical
   1

ispcp = logical
   0

Create an array x whose elements are 9=3×3, 15=3×5, and 25=5×5. Verify that all elements of x are coprime.

x = [9 15 25];

Verify that at least one pair of elements of x has a greatest common divisor greater than 1. Output the pairs.

[~,ispcp,pidx] = iscoprime(x);
disp(pidx)
     1     1     2
     2     3     3

Input Arguments

collapse all

Input array, specified as a row vector of positive integers.

Example: [21 36 49]

Data Types: single | double

Output Arguments

collapse all

Returns true if all elements of x are coprimes.

Returns true If any two elements of x are pairwise coprime. Returns false if no elements of x are pairwise coprime.

Array pair indices, returned as a two-row integer-valued matrix. pidx has N-choose-2 (N2)=12N(N1) columns. Each column of pidx specifies the indices of a pair of elements in x.

Greatest common divisors of element pairs, returned as a row vector with a number of elements equal to the number of columns of pidx. Each element of pgcd is the greatest common divisor of the two elements of x identified by the indices in the corresponding column of pidx.

More About

collapse all

Coprimes

Two integers m and n are coprime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides m does not divide n, and vice versa. This is equivalent to their greatest common divisor (gcd) being 1. has no two elements whose greatest common divisor is greater than 1. ispcp is false if any two elements are coprime.

For example, the numbers 14 and 9 are coprime since their only common divisor is 1. Note that neither number is a prime number. On the other hand, 15 and 9 are not coprime, because they both are divisible by 3. By definition, the numerator and denominator of a reduced fraction are coprime. The numbers in the set [15,9,11] are coprime while they are not pairwise coprime.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2021a

See Also

| |