1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Add functions gcd() and lcm() for integer and numeric types.

These compute the greatest common divisor and least common multiple of
a pair of numbers using the Euclidean algorithm.

Vik Fearing, reviewed by Fabien Coelho.

Discussion: https://postgr.es/m/adbd3e0b-e3f1-5bbc-21db-03caf1cef0f7@2ndquadrant.com
This commit is contained in:
Dean Rasheed
2020-01-25 14:00:59 +00:00
parent 530609aa42
commit 13661ddd7e
12 changed files with 689 additions and 1 deletions

View File

@ -870,6 +870,40 @@
<entry><literal>-43</literal></entry>
</row>
<row>
<entry>
<indexterm>
<primary>gcd</primary>
</indexterm>
<literal><function>gcd(<replaceable>a</replaceable>, <replaceable>b</replaceable>)</function></literal>
</entry>
<entry>(same as argument types)</entry>
<entry>
greatest common divisor (the largest positive number that divides both
inputs with no remainder); returns <literal>0</literal> if both inputs
are zero
</entry>
<entry><literal>gcd(1071, 462)</literal></entry>
<entry><literal>21</literal></entry>
</row>
<row>
<entry>
<indexterm>
<primary>lcm</primary>
</indexterm>
<literal><function>lcm(<replaceable>a</replaceable>, <replaceable>b</replaceable>)</function></literal>
</entry>
<entry>(same as argument types)</entry>
<entry>
least common multiple (the smallest strictly positive number that is
an integral multiple of both inputs); returns <literal>0</literal> if
either input is zero
</entry>
<entry><literal>lcm(1071, 462)</literal></entry>
<entry><literal>23562</literal></entry>
</row>
<row>
<entry>
<indexterm>