Documentation/Calc Functions/GCD

From The Document Foundation Wiki
Jump to navigation Jump to search
Other languages:
English • ‎Nederlands • ‎dansk • ‎español



Function name:

GCD

Category:

Mathematical

Summary:

Calculates the greatest common divisor (GCD) of a set of positive integers. The greatest common divisor is the largest positive integer that will divide, without remainder, each of the given integer values.

Syntax:

GCD(Integer 1 [; Integer 2 [; … [; Integer 255]]])

Returns:

Returns a non-negative integer that is the greatest common divisor of the supplied numbers.

Arguments:

Integer 1, Integer 2, … , Integer 255 give the set of positive integers for which the greatest common divisor is to be calculated. At least one value must be provided. Each argument may take one of the following forms:

  • A positive integer, or an expression that evaluates to a positive integer.
  • A reference to a single cell containing a positive integer.
  • A simple reference to a cell range containing positive integers (for example, A1:B9).
  • The name of a named range, comprising cells containing positive integers.
  • The name of a database range, comprising cells containing positive integers.
  • An inline array of positive integers (for example, {1, 2, 3, 4}).

Note that although GCD can accept up to 255 arguments, each argument could specify a range of cells. This means that the number of positive integers processed could be many more than 255.

The following conditions (including errors) may be encountered:

  • If any argument is a string in quotation marks, or a reference to a single cell that contains a string rather than a positive integer, then GCD reports a #VALUE! error.
  • Text and empty cells within a data range are ignored.
  • If any value is negative, then GCD reports an invalid argument error (Err:502).
  • If any value is not an integer, that value is truncated to its floor value.
  • If after any truncation all supplied values are equal to 0, then GCD returns the value 0.

Additional details:

  • GCD ignores any text or empty cells within a data range. If you suspect wrong results, look for text within the supplied data range(s). To highlight text content within a data range, use Calc's View ▸ Value Highlighting feature.
  • For two positive integers i and j, the following relationship holds:

[math]\displaystyle{ i\times j~=~\text {LCM}(i, j) \times \text{GCD}(i, j) }[/math]

LCM(i, j) is the least common multiple of i and j (see LCM).
  • The greatest common divisor (GCD) is sometimes referred to as the greatest common factor (GCF) or the highest common factor (HCF).

Examples:

Formula Description Returns
=GCD(16; 32; 24; 40) Here the function returns 8, because that is the largest positive integer that can divide 16, 24, 32, and 40 without a remainder. 8
=GCD(B1:B3), where cells B1, B2, and B3 contain the numbers 9, 12, and 6 respectively. Here the function returns 3, because that is the largest positive integer that can divide 6, 9, and 12 without a remainder. The formula =GCD(B1; B2; B3) would give the same result. 3
=GCD(3.2; 5.6) The two argument values are truncated to 3 and 5 respectively. Here the function returns 1, because that is the largest positive integer that can divide 3 and 5 without a remainder. 1
=GCD({2; 4; 6; 8}) Here the function returns 2, because that is the largest positive integer that can divide 2, 4, 6, and 8 without a remainder. 2

Related LibreOffice functions:

GCD_EXCEL2003

LCM

ODF standard:

Section 6.16.36, part 2

Equivalent Excel functions:

GCD