## Posted By

Scooter on 06/14/08

## Who likes this?

3 people have marked this snippet as a favorite

# Factorial

/ Published in: PHP

`<?php    /*    PHP Mathematics Library - Factorial        Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.        This work is licensed under the Creative Commons Attribution License. To view    a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or    send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California    94305, USA.    */     // Determine the factorial for a given number    function factorial(\$x)    {        \$result = 1;         if (\$x > 1)        {            for (\$i = 2; \$i <= \$x; \$i++)            {                \$result *= \$i;            }        }         return \$result;    }     // Returns the number of combinations, without regard to order, of y items that can be made from a pool of x items.    // Requires factorial()    function combinatorial(\$x, \$y)    {        return ((\$x >= \$y) && (\$y > 0)) ? factorial(\$x) / factorial(\$y) / factorial(\$x - \$y) : 0;    }     // Returns the number of permutations, with regard to order, of y items that can be made from a pool of x items.    // Requires factorial()    function permutations(\$x, \$y)    {        return factorial(\$x) / factorial(\$x - \$y);    }?>`