PHP Classes

PHP Permutation and Combination: Compute permutations and combinations of values

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 127 All time: 9,384 This week: 571Up
Version License PHP version Categories
permutation-combine 1.0.0Freeware5PHP 5, Statistics
Description 

Author

This class compute permutations and combinations of given values.

It can take as parameters a given number of items or a number of items to pick.

The class can compute the total number of possible combinations or permutations of items either allowing repetitions or not.

Picture of Anthony Amolochitis
  Performance   Level  
Name: Anthony Amolochitis <contact>
Classes: 10 packages by
Country: United States United States
Age: 44
All time rank: 1138166 in United States United States
Week rank: 411 Up48 in United States United States Up

Example

<?php


class PermutationCombination
{
   
/**
     * @var int
     */
   
public $nItems = 0 ;
   
   
/**
     * @var int
     */
   
public $rPicked = 0 ;
       
   
/**
     * Compute the N factorial
     * @return int
     */
   
protected function Generate_N_Factorial()
    {
       
$num = $this->nItems ;
       
$factorial = 1 ;
        for(
$x = $num ; $x >= 1 ; $x-- )
        {
           
$factorial = $factorial * $x ;
        }
        return
$factorial ;
    }
   
   
/**
     * Compute the R factorial
     * @return int
     */
   
protected function Generate_R_Factorial()
    {
       
$num = $this->rPicked ;
       
$factorial = 1 ;
        for(
$x = $num ; $x >= 1 ; $x-- )
        {
           
$factorial = $factorial * $x ;
        }
        return
$factorial ;
    }
   
   
/**
     * Compute the N factorial
     * @return int
     */
   
protected function Generate_N_Minus_R_Factorial()
    {
       
$num = $this->nItems - $this->rPicked ;
       
$factorial = 1 ;
        for(
$x = $num ; $x >= 1 ; $x-- )
        {
           
$factorial = $factorial * $x ;
        }
        return
$factorial ;
    }
   
   
/**
     * Compute the (N + R - 1) factorial
     * @return int
     */
   
protected function Generate_N_Plus_R_MinusOne_Factorial()
    {
       
$num = ( $this->nItems + $this->rPicked - 1 ) ;
       
$factorial = 1 ;
        for(
$x = $num ; $x >= 1 ; $x-- )
        {
           
$factorial = $factorial * $x ;
        }
        return
$factorial ;
    }
   
   
/**
     * Compute the (N + R - 1) factorial
     * @return int
     */
   
protected function Generate_N_Minus_One_Factorial()
    {
       
$num = ( $this->nItems - 1 ) ;
       
$factorial = 1 ;
        for(
$x = $num ; $x >= 1 ; $x-- )
        {
           
$factorial = $factorial * $x ;
        }
        return
$factorial ;
    }
   
   
/**
     * nPr
     * Ordering matters.
     * a and b are different that b and a.
     * @return float
     */
   
public function ComputePermutations()
    {
       
$nFactorial = $this->Generate_N_Factorial();
       
$n_Minus_r_Factorial = $this->Generate_N_Minus_R_Factorial() ;
       
        return (
$nFactorial / $n_Minus_r_Factorial );
    }
   
   
/**
     * Ordering does not matter
     * a and b are different that b and a.
     * @return float
     */
   
public function ComputePermutations_AllowRepitition()
    {
        return
pow($this->nItems, $this->rPicked);
    }
   
   
/**
     * nCr
     * Order does not matter.
     * a and b is the same as b and a
     * @return float
     */
   
public function ComputeCombinations()
    {
       
$nFactorial = $this->Generate_N_Factorial();
       
$rFactorial = $this->Generate_R_Factorial() ;
       
$n_Minus_r_Factorial = $this->Generate_N_Minus_R_Factorial() ;
       
        return (
$nFactorial / ($n_Minus_r_Factorial * $rFactorial) );
    }
   
   
    public function
ComputeCombination_RepititionAllowed()
    {
       
$numerator = $this->Generate_N_Plus_R_MinusOne_Factorial();
       
$rFactorial = $this->Generate_R_Factorial() ;
       
$nMinusOneFactorial = $this->Generate_N_Minus_One_Factorial();
       
        return (
$numerator / ($rFactorial * $nMinusOneFactorial) );
    }
   
}


$t = new PermutationCombination();
$t->nItems = 5 ;
$t->rPicked = 5 ;

echo
'Permutations: ' ;
echo
$t->ComputePermutations() ;

echo
'<br/>';
echo
'Permutations Allow Repitition: ' ;
echo
$t->ComputePermutations_AllowRepitition() ;


echo
'<br/>';
echo
'Combinations: ' ;
echo
$t->ComputeCombinations() ;

echo
'<br/>';
echo
'Combinations with Repitition: ' ;
echo
$t->ComputeCombination_RepititionAllowed() ;


?>


  Files folder image Files  
File Role Description
Plain text file ExampleUsage.php Example Example Usage of the class
Plain text file PermutationCombination.php Class Compute Combinations and Permutations

 Version Control Unique User Downloads Download Rankings  
 0%
Total:127
This week:0
All time:9,384
This week:571Up