Contents

GMP Functions

Basic GMP Functions

gmp_abs()
Description: Calculates the absolute value of a GMP number.
Example:

				
					$result = gmp_abs("-42");
// Output: (absolute value is 42)

				
			

gmp_add()
Description: Adds two GMP numbers.
Example:

				
					$result = gmp_add("123", "456");
// Output: (sum is 579)

				
			

gmp_clrbit()
Description: Sets the bit at a specified index in a GMP number to 0.
Example:

				
					$num = gmp_init("15");
gmp_clrbit($num, 1);
// Output: (number becomes 13 after clearing bit 1)

				
			

gmp_cmp()
Description: Compares two GMP numbers.
Example:

				
					$result = gmp_cmp("100", "200");
// Output: (-1 because 100 < 200)

				
			

gmp_com()
Description: Calculates the one’s complement of a GMP number.
Example:

				
					$result = gmp_com("5");
// Output: (-6, the one's complement of 5)

				
			

gmp_div_q()
Description: Performs division of GMP numbers, returning the quotient.
Example:

				
					$result = gmp_div_q("10", "3");
// Output: (quotient is 3)

				
			

gmp_div_qr()
Description: Performs division and returns both quotient and remainder.
Example:

				
					$result = gmp_div_qr("10", "3");
// Output: ([quotient => 3, remainder => 1])

				
			

gmp_div_r()
Description: Performs division and returns only the remainder.
Example:

				
					$result = gmp_div_r("10", "3");
// Output: (remainder is 1)

				
			

gmp_divexact()
Description: Divides exactly when one GMP number is divisible by another.
Example:

				
					$result = gmp_divexact("10", "2");
// Output: (exact quotient is 5)

				
			

gmp_export()
Description: Exports a GMP number to a binary string.
Example:

				
					$result = gmp_export("255");
// Output: ("\xFF" - binary string representation of 255)

				
			

gmp_fact()
Description: Calculates the factorial of a GMP number.
Example:

				
					$result = gmp_fact("5");
// Output: (factorial is 120)

				
			

gmp_gcd()
Description: Calculates the GCD of two GMP numbers.
Example:

				
					$result = gmp_gcd("48", "18");
// Output: (GCD is 6)

				
			

gmp_gcdext()
Description: Calculates the GCD and multipliers for the equation.
Example:

				
					$result = gmp_gcdext("48", "18");
// Output: ([gcd => 6, s => -1, t => 3])

				
			

gmp_hamdist()
Description: Finds the Hamming distance between two GMP numbers.
Example:

				
					$result = gmp_hamdist("5", "7");
// Output: (Hamming distance is 1)

				
			

gmp_import()
Description: Imports a GMP number from a binary string.
Example:

				
					$result = gmp_import("\xFF");
// Output: (GMP number is 255)

				
			

gmp_intval()
Description: Converts a GMP number to an integer.
Example:

				
					$result = gmp_intval("123456789");
// Output: (integer is 123456789)

				
			

gmp_invert()
Description: Finds the modular inverse of a GMP number.
Example:

				
					$result = gmp_invert("3", "7");
// Output: (modular inverse is 5)

				
			

gmp_jacobi()
Description: Computes the Jacobi symbol of two GMP numbers.
Example:

				
					$result = gmp_jacobi("3", "7");
// Output: (Jacobi symbol is 1)

				
			

gmp_legendre()
Description: Computes the Legendre symbol of two GMP numbers.
Example:

				
					$result = gmp_legendre("3", "7");
// Output: (Legendre symbol is 1)

				
			

gmp_mod()
Description: Finds the modulo of a GMP number.
Example:

				
					$result = gmp_mod("10", "3");
// Output: (modulo is 1)

				
			

gmp_mul()
Description: Multiplies two GMP numbers.
Example:

				
					$result = gmp_mul("123", "456");
// Output: (product is 56088)

				
			

gmp_neg()
Description: Returns the negative of a GMP number.
Example:

				
					$result = gmp_neg("42");
// Output: (-42)

				
			

gmp_nextprime()
Description: Finds the smallest prime number greater than the given GMP number.
Example:

				
					$result = gmp_nextprime("100");
// Output: (next prime is 101)

				
			

Gmagick::medianFilterImage()
Description: Reduces noise in the image using a median filter.
Example:

				
					$image->medianFilterImage(2);  
// Output: (reduces image noise with a radius of 2)

				
			

gmp_perfect_square()
Description: Checks if the given GMP number is a perfect square.
Example:

				
					$result = gmp_perfect_square("49");
// Output: (true, because 49 is a perfect square)

				
			

gmp_popcount()
Description: Finds the population count (number of set bits) in a GMP number.
Example:

				
					$result = gmp_popcount("15");
// Output: (population count is 4)

				
			

gmp_pow()
Description: Raises a GMP number to the power of an integer.
Example:

				
					$result = gmp_pow("2", "10");
// Output: (1024)

				
			

gmp_prob_prime()
Description: Checks the probability of a GMP number being prime.
Example:

				
					$result = gmp_prob_prime("101");
// Output: (value 2, meaning 101 is definitely prime)

				
			

gmp_random_bits()
Description: Generates a random number with a specified number of bits.
Example:

				
					$result = gmp_random_bits(10);
// Output: (random number with up to 10 bits, e.g., 745)

				
			

gmp_random_range()
Description: Generates a random number within a specified range.
Example:

				
					$result = gmp_random_range("1", "100");
// Output: (random number between 1 and 100)

				
			

gmp_random_seed()
Description: Sets the seed for the random number generator.
Example:

				
					gmp_random_seed("123");
// Output: (sets the RNG seed to 123)

				
			

gmp_root()
Description: Calculates the integer part of the N-th root of a GMP number.
Example:

				
					$result = gmp_root("27", "3");
// Output: (cube root is 3)

				
			

gmp_rootrem()
Description: Calculates the N-th root and the remainder.
Example:

				
					list($root, $remainder) = gmp_rootrem("28", "3");
// Output: (root is 3, remainder is 1)

				
			

gmp_scan0()
Description:
Scans for the first occurrence of the bit “0” in a GMP number.
Example:

				
					$result = gmp_scan0("15", 1);
// Output: (first 0-bit is at index 4)

				
			

gmp_scan1()
Description:
Scans for the first occurrence of the bit “1” in a GMP number.
Example:

				
					$result = gmp_scan1("8", 0);
// Output: (first 1-bit is at index 3)

				
			

gmp_setbit()
Description: Sets a specific bit in a GMP number.
Example:

				
					$num = gmp_init("0");
gmp_setbit($num, 3);
// Output: (number becomes 8)

				
			

gmp_sign()
Description: Checks the sign of a GMP number.
Example:

				
					$result = gmp_sign("-42");
// Output: (-1 because the number is negative)

				
			

gmp_sqrt()
Description: Calculates the square root of a GMP number.
Example:

				
					$result = gmp_sqrt("49");
// Output: (square root is 7)

				
			

gmp_sqrtrem()
Description: Calculates the square root and remainder of a GMP number.
Example:

				
					list($sqrt, $remainder) = gmp_sqrtrem("50");
// Output: (square root is 7, remainder is 1)

				
			

gmp_strval()
Description: Returns the string representation of a GMP number.
Example:

				
					$result = gmp_strval("12345");
// Output: ("12345")

				
			

gmp_sub()
Description: Subtracts one GMP number from another.
Example:

				
					$result = gmp_sub("100", "42");
// Output: (difference is 58)

				
			

gmp_testbit()
Description: Checks if a specific bit in a GMP number is set.
Example:

				
					$result = gmp_testbit("8", 3);
// Output: (true because bit 3 is set)

				
			

gmp_xor()
Description: Calculates the XOR of two GMP numbers.
Example:

				
					$result = gmp_xor("10", "5");
// Output: (result is 15)

				
			

gmp_random()
Description: Generates a random GMP number.
Example:

				
					$result = gmp_random(5);
// Output: (random number with 5 limbs)