Understanding Postgres LCM() and GCD() Functions Using Examples

PostgreSQL offers many mathematical functions that assist us in performing mathematical computations. These functions include; cbrt(), factorial(), log(), pi() and round() etc. Among these mathematical functions, the most frequently used are LCM() and GCD().

In this article, we will be discussing two mathematical functions i.e. LCM() and GCD() in detail with the help of examples.

Understanding Postgres LCM() in PostgreSQL

The lcm() function is used to get the lowest common multiples of two specified numbers. The basic syntax of the lcm() function is:

lcm(num1, num2)

In the above syntax:

● “num1” and “num2” are the integers/numbers of which we want to get the lcm. Both of these should be of numeric type, otherwise, the function will display an error.

● The function returns the lowest common multiple of both the specified numbers that are also of numeric type. The function returns value NULL if any of the values is NULL.

Let’s observe some example queries to understand how the function works.

Example: How Does LCM() Work in Postgres?

Let’s consider the following queries:

SELECT
lcm(4, 5) AS "lcm_1",
lcm(24, 40) AS "lcm_2",
lcm(7.3, 5.7) AS "lcm_3";

The above query will simply return the LCMs of the specified numbers in the function. The output of the function will be:

img

So we can notice that the function has given the lcm of the specified numbers. This is how the lcm() function works in PostgreSQL.

Moving on to the GCD() function let’s see how it works.

Understanding Postgres GCD() in PostgreSQL

The GCD() function is used to get the greatest common divisor of two numbers provided as arguments/parameters. The basic syntax of the gcd() function is:

gcd(num1, num2)

In the above syntax:

● “num1” and “num2” are the integers/numbers of which we want to get the greatest common divisor. Both of these should be of numeric type. Otherwise, the function will display an error.

● The function gives the greatest common divisor of both the specified numbers that are also of numeric type. The function returns value NULL if any of the values is NULL.

Let’s observe some example queries to understand how the function works.

Example: How Does the GCD() Work in Postgres?

We now will observe the below example to understand how the gcd() function works.

SELECT
gcd(4, 5) AS "gcd_1",
gcd(24, 40) AS "gcd_2",
gcd(6.3, 9.6) AS "gcd_3";

The above query will simply return the GCDs of the specified numbers in the function. The output of the function will be:

img

So, we can note that the function has given the gcd of the specified numbers. This is how the gcd() function works in PostgreSQL.

This write-up demonstrated the working of lcm() and gcd() functions in PostgreSQL.

Conclusion

The lcm() and gcd() functions are the commonly used mathematical functions. Both the functions take two numeric type parameters and return their lowest common factor and the greatest common divisor respectively. The content of this article comprised of the details of these two functions with their practical examples.