PHP | gmp_perfect_square() Function
Last Updated :
27 Nov, 2018
The gmp_perfect_square() is an inbuilt function in PHP which checks if the given GMP number(GNU Multiple Precision: For large numbers) is a perfect square or not.
Syntax:
gmp_perfect_square($num)
Parameters: The function accepts one GMP number $num. This parameter can be a GMP object in PHP version 5.6 and later, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.
Return Value: The function returns true if the given number $num is a perfect square, otherwise it returns false.
Examples:
Input : $num=25
Output : true
Input : $num=10
Output : false
Below programs illustrate the use of gmp_perfect_square() function:
Program 1: The program below demonstrates the working of gmp_perfect_square() function when GMP number is passed as an argument.
<?php
$num = gmp_init( "1001" , 2);
var_dump(gmp_perfect_square( $num )). "\n" ;
$num = gmp_init( "11001" , 2);
var_dump(gmp_perfect_square( $num )). "\n" ;
$num = gmp_init( "1100" , 2);
var_dump(gmp_perfect_square( $num ));
?>
|
Output:
bool(true)
bool(true)
bool(false)
Program 2: The program below demonstrates the working of gmp_perfect_square() when numeric string is passed as an argument.
<?php
$num = "9" ;
var_dump(gmp_perfect_square( $num )). "\n" ;
$num = "25" ;
var_dump(gmp_perfect_square( $num )). "\n" ;
$num = "12" ;
var_dump(gmp_perfect_square( $num ));
?>
|
Output:
bool(true)
bool(true)
bool(false)
Reference:
http://php.net/manual/en/function.gmp-perfect-square.php
Share your thoughts in the comments
Please Login to comment...