Last Updated:

Math functions in PHP with example

 

Calculates the factorial.


Syntax:


resource gmp_fact (resourse x)
Returns the factorial of the number specified in the x parameter.

gmp_sqrt

Calculates the square root.


Syntax:


resource gmp_sqrt (resourse x)
Returns the square root of the number specified in the x parameter.

 

gmp_sqrtrm

Calculates the square root with the remainder.


Syntax:


array gmp_sqrtrm (resourse x)


This function returns an array in which the element with index [0] is the square root of the argument, the element with index [1] is the difference between the argument and the element [0] squared.

gmp_perfect_square

 

Determines whether a number is a complete square.


Syntax:


bool gmp_perfect_square (resourse x)
The gmp_perfect_square function () will return true if x is the square of an integer. Otherwise, it will return false.

gmp_pow

 

Exponentiation.


Syntax:


resource gmp_pow (resourse x, int y)
This function returns a result equal to raising the argument x to the power of y, provided that y is not negative.
echo gmp_pow (2,3); Outputs 8
echo gmp_pow (0.0); Displays 1
gmp_powm
gmp_prob_prime

 

Check "probably" a prime number.


Syntax:


int gmp_prob_prime (resourse x [, int reps])
This function will return 0 if x is a complex number, i.e. having more than two integer divisors. Returns 1 if x is possibly a prime number. If it returns 2, then x is probably a prime number.


The reps argument specifies the quality of the check. The larger this number, the more accurate the result. Can take values from 5 to 10 (default).


This function uses the Miller-Rabin probabilistic test algorithm.

gmp_gcd

Finds the largest common divisor.


Syntax:


resource gmp_gcd (resourse x, resource y)
Always returns a positive result.

gmp_gcdext

Finding the largest common divisor with multipliers.


Syntax:


array gmp_gcdext (resourse x, resource y)
The function gmp_gcdext () returns an array with values g,s,t such that x*s+y*t=g=NOD(x,y), gdn GCD is the largest common divisor.

gmp_invert

 

Produces inversion modulo.


Syntax:


resource gmp_invert (resourse x, resource y)
The function returns the addition of x to a value divisible by a target of y. In case the result cannot be found, returns false.

gmp_legendre

Returns a Legrange number


Syntax:


int gmp_legendre (resourse x, resource p)
function returns a Legrange number. p should be an even positive.

gmp_jacobi

Returns a Jacobi number.
Syntax:
int gmp_jacobi (resourse x, resource p)


function returns a Jacobi number. p should be an even positive.

gmp_random

Generates a random number.


Syntax:


Resource gmp_random (int limited)
limited specifies the length of the generated number. In case the value of limited is negative, a negative number is generated.

gmp_popcount

 

Getting a population.


Syntax:
int gmp_popcount (resource x)
function returns a population numerator.

gmp_hamdist

 

Calculation of the distance.


Syntax:


int gmp_hamdist (resource x, resource y)
The function returns the distance between the numbers x and y. Arguments x and y must be non-negative.