useful recursive PHP functions

recursively ...

PHP Manual for

XXXVI. GMP Functions

Introduction

These functions allow you to work with arbitrary-length integers using the GNU MP library.

These functions have been added in PHP 4.0.4.

Note: Most GMP functions accept GMP number arguments, defined as resource below. However, most of these functions will also accept numeric and string arguments, given that it is possible to convert the latter to a number. Also, if there is a faster function that can operate on integer arguments, it would be used instead of the slower function when the supplied arguments are integers. This is done transparently, so the bottom line is that you can use integers in every function that expects GMP number. See also the gmp_init() function.

Warning

If you want to explicitly specify a large integer, specify it as a string. If you don't do that, PHP will interpret the integer-literal first, possibly resulting in loss of precision, even before GMP comes into play.

Note: This extension is not available on Windows platforms.

Requirements

You can download the GMP library from http://www.swox.com/gmp/. This site also has the GMP manual available.

You will need GMP version 2 or better to use these functions. Some functions may require more recent version of the GMP library.

Installation

In order to have these functions available, you must compile PHP with GMP support by using the --with-gmp option.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

Resource Types

This extension has no resource types defined.

Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

GMP_ROUND_ZERO ( integer)

GMP_ROUND_PLUSINF ( integer)

GMP_ROUND_MINUSINF ( integer)

Examples

Example 1. Factorial function using GMP

<?php
function fact($x)
{
    if (
$x <= 1) {
        return
1;
    } else {
        return
gmp_mul($x, fact($x-1));
    }
}

echo
gmp_strval(fact(1000)) . "\n";
?>

This will calculate factorial of 1000 (pretty big number) very fast.

See Also

More mathematical functions can be found in the sections BCMath Arbitrary Precision Mathematics Functions and Mathematical Functions.

Table of Contents gmp_abs -- Absolute valuegmp_add -- Add numbersgmp_and -- Logical ANDgmp_clrbit -- Clear bitgmp_cmp -- Compare numbersgmp_com -- Calculates one's complementgmp_div_q -- Divide numbersgmp_div_qr -- Divide numbers and get quotient and remaindergmp_div_r -- Remainder of the division of numbersgmp_div -- Alias of gmp_div_q() gmp_divexact -- Exact division of numbersgmp_fact -- Factorialgmp_gcd -- Calculate GCDgmp_gcdext -- Calculate GCD and multipliersgmp_hamdist -- Hamming distancegmp_init -- Create GMP numbergmp_intval -- Convert GMP number to integergmp_invert -- Inverse by modulogmp_jacobi -- Jacobi symbolgmp_legendre -- Legendre symbolgmp_mod -- Modulo operationgmp_mul -- Multiply numbersgmp_neg -- Negate numbergmp_or -- Logical ORgmp_perfect_square -- Perfect square checkgmp_popcount -- Population countgmp_pow -- Raise number into powergmp_powm -- Raise number into power with modulogmp_prob_prime -- Check if number is "probably prime"gmp_random -- Random numbergmp_scan0 -- Scan for 0gmp_scan1 -- Scan for 1gmp_setbit -- Set bitgmp_sign -- Sign of numbergmp_sqrt -- Calculate square rootgmp_sqrtrem --  Square root with remaindergmp_strval -- Convert GMP number to stringgmp_sub -- Subtract numbersgmp_xor -- Logical XOR

© 2012 lixlpixel