Open In App

PHP | gmp_perfect_square() Function

Last Updated : 27 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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
// PHP program to check the if the 
// number is perfect square or not
  
// numeric string arguments 
$num = gmp_init("1001", 2);
// checks if 9 (1001) is a perfect number or not
var_dump(gmp_perfect_square($num))."\n";  
  
  
$num = gmp_init("11001", 2);
// checks if 25 (11001) is a perfect number or not
var_dump(gmp_perfect_square($num))."\n";   
  
$num = gmp_init("1100", 2);
// checks if 12 (1100) is a perfect number or not
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
// PHP program to check the if the 
// number is perfect square or not
  
// numeric string arguments 
$num = "9";
// checks if 9 (1001) is a perfect number or not
var_dump(gmp_perfect_square($num))."\n";  
  
  
$num = "25";
// checks if 25 (11001) is a perfect number or not
var_dump(gmp_perfect_square($num))."\n";   
  
$num = "12";
// checks if 12 (1100) is a perfect number or not
var_dump(gmp_perfect_square($num));  
?>


Output:

bool(true) 
bool(true)
bool(false)

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads