Thursday, December 11, 2025
HomeLanguagesPHP gmp_lcm() Function

PHP gmp_lcm() Function

The gmp_lcm() is an inbuilt function in PHP that is used to calculate the least common multiple (LCM) of two or more integers.

Syntax:

 gmp_lcm(GMP|int|string $num1, GMP|int|string $num2): GMP

Parameters: This function accepts two parameters that are described below.

  • $num1: A GMP number resource representing the first integer.
  • $num2: A GMP number resource representing the second integer.

Return Values: The gmp_lcm() function returns the lcm of two integers which is provided by the user.

Program 1: The following program demonstrates gmp_lcm() function.

PHP




<?php
    
// Assuming you have the GMP
// extension enabled
$a = gmp_init(12);
$b = gmp_init(18);
$lcm = gmp_lcm($a, $b);
echo "LCM of $a and $b is: $lcm\n";
?>


Output:

LCM of 12 and 18 is: 36

Program 2: The following program demonstrates gmp_lcm() function.

PHP




<?php
  
// Assuming you have the GMP 
// extension enabled
$a = gmp_init(12);
$b = gmp_init(18);
$c = gmp_init(24);
  
$lcm_of_three = gmp_lcm($a, gmp_lcm($b, $c));
  
echo "LCM of $a, $b, and $c is: $lcm_of_three\n";
  
?>


Output:

LCM of 12, 18, and 24 is: 72

Reference: https://www.php.net/manual/en/function.gmp-lcm.php

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32440 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6811 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12023 POSTS0 COMMENTS
Shaida Kate Naidoo
6943 POSTS0 COMMENTS
Ted Musemwa
7195 POSTS0 COMMENTS
Thapelo Manthata
6889 POSTS0 COMMENTS
Umr Jansen
6880 POSTS0 COMMENTS