PostgreSQL 18 reference · Math Functions
PostgreSQL GCD() Function
Return the greatest common divisor of two integers, the largest number that divides both. lcm() gives the least common multiple.
PostgreSQL 18 Since 13
Syntax
gcd(a, b) | lcm(a, b)
Parameters
| Parameter | Description |
|---|---|
a, b |
Integers or numerics. |
Return value
A number of the input type.
Examples
SELECT gcd(12, 18) AS gcd, lcm(4, 6) AS lcm;
Output
gcd | lcm -----+----- 6 | 12 (1 row)
Run this on your computer — see “Get Started” for setup.
SELECT 12 / gcd(12, 18) || '/' || 18 / gcd(12, 18) AS simplified;
Output
simplified ------------ 2/3 (1 row)
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| PostgreSQL 13 | Added |
Related
MOD()· Return the remainder of dividing one number by another; x % y does the same. The sign follows the dividend.- SQL Aggregate Functions · tutorial