HomeLanguagesPHP | gmp_gcd() Function

PHP | gmp_gcd() Function

The gmp_gcd() is an in built function in PHP which is used to calculate the GCD of 2 GMP numbers (GNU Multiple Precision : For large numbers).

Syntax:

gmp_gcd ( $num1, $num2 )

Parameters: This function accepts two GMP numbers $num1 and $num2 as parameters. This function calculates the GCD of these two numbers.

Return Value: This function returns a positive GMP number which is the GCD of $num1 and $num2.

Examples:

Input : gmp_gcd("12", "21")
Output : 3

Input : gmp_gcd("15", "30")
Output : 15

Explanation: In the above example gmp_gcd() function calculates the greatest common divisor of num1 and num2. The result is always positive even if either of, or both, input operands are negative.

Below programs illustrate the gmp_gcd() function in PHP.

Program 1:




<?php
  
$gcd = gmp_gcd("12", "21");
echo gmp_strval($gcd) . "\n";
  
?>


Output:

3

Program 2:




<?php
  
$gcd = gmp_gcd("15", "30");
echo gmp_strval($gcd) . "\n";
  
?>


Output:

15

Reference:
http://php.net/manual/en/function.gmp-gcd.php

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS