Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.1.0 - Français

Change language to:
English - 日本語 - Português - Русский

Please note that the recommended version of Scilab is 2024.1.0. This page might be outdated.
See the recommended documentation of this function

Aide de Scilab >> Fonctions Elémentaires > Arithmétique > gcd


Greatest (positive) Common Divisor


gpcd = gcd(P)
[gpcd, U] = gcd(P)



array of decimal integers, encoded integers, or of polynomials.


single element of P type: the greatest common divisor of all P components.


Square matrix of the P type, with integer components or of minimal degrees. Its last column B = U(:,$) holds Bezout coefficients, such that B(1)*P(1) + B(2)*P(2) + .... + B($)*P($) == gpcd.


[gpcd, U] = gcd(P) computes the greatest common divisor gpcd of components of P, and an unimodular matrix U.

If P components are decimal or encoded integers, they are priorly converted into int64 signed integers.

If P has an unsigned inttype uint8, uint16, uint32 or uint64, U gets the corresponding signed inttype.

When P are integers, the returned GCD is always positive.

When a second output is expected, an unimodular matrix U of the P type is returned, such that

  • size(U) == [length(P) length(P)]
  • matrix(P,1,-1)*U = [0...0 gpcd] with length(P)-1 leading zeros
  • det(U) is 1 or -1.

Its last column provides Bezout coefficients.

gcd([0 0]) returns 0.
For big P values (smaller but of the order of 2^63, depending also on the number of input values), results may be corrupted by integer overflow and wrapping (int8(127)+1 == -128).


// With polynomials
s = %s;
p = [s  s*(s+1)^2  2*s^2+s^3];
[GCD, U] = gcd(p)

// With encoded integers
V = uint16([2^2*3^5 2^3*3^2 2^2*3^4*5])
[GCD, U] = gcd(V)

// With decimal integers
V = [2^2*3^5 2^3*3^2 2^2*3^4*5];
[GCD, U] = gcd(V)

gcd([0 60])
gcd([0 0])

See also

  • bezout — équation de Bezout pour les polynômes
  • lcm — Plus petit multiple commun (PPCM) de polynômes ou d'entiers
  • factor — factor function
  • prod — produit des éléments d'un tableau.
  • hermit — forme d'Hermite


  • int64 and uint64 input integers are now supported.
  • The input P may be any array instead of a row vector.
  • For input integers possibly negative, the returned GCD is now always positive.
Report an issue
<< factorial Arithmétique lcm >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Tue Feb 25 08:50:19 CET 2020