Today we are going to discuss Variations, Permutations and Combinations.
Difference Between Combinations and Permutations
In the English language, the word COMBINATION is used rather loosely, but mathematics requires a more complete definition so in math, there are both COMBINATIONS and PERMUTATIONS and the difference is as follows:
A COMBINATION is a set of items without any particular order. In a combination, the number set 1,2,3 would be equal to the set 3,1,2.
A PERMUTATION is a set of items where order matters. In a permutation the number set 1,2,3, the set 2,1,3 , the set 3,2,1 and the set 3,1,2 are all separate and not equal to one another as they are in a combination.
Combination vs. Permutation
Combinations:
As you can see, there are only 9 combinations of 123. If you look to the right, you can see that there are 27 Permutations because the number of results increases when order matters. |
Permutations:
|
Some People Call Permutations and/or Combinations VARIATIONS
VARIATIONS or VARIATION is an even more confusing word if used in the context of mathematics and therefore should probably not be used in the same context as combinations and variations. In common English, one of several similar definitions for variation is: Something slightly different from another of the same type. However in mathematics, a variation is often defined as: A function that relates the values of one variable to those of other variables. Confused? If not, great, but I am sure you can see why we should stick to the words Combination and Permutation in this context none-the-less.
PHP PERMUTATIONS
I wrote a simple script to display all possible 3 letter long PERMUTATIONS in PHP. Here it is:
<?php
$alpha = array(‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’,’k’,’l’,’m’,’n’,’o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’);
$z=0;
$doms = ”;
for ($i=0; $i<26; $i++){
for ($j=0; $j<26; $j++){
for ($k=0; $k<26; $k++){
$z++;
$doms .= $alpha[$i] . $alpha[$j] . $alpha[$k] . “<br />”;
}
}
}
echo “<h1>Found $z 3 letter long permutations:</h1>$doms”;
?>
If you examine the above code, you’ll notice there is three PHP for loops. Therefore, if you wanted the script to return only two letter strings, remove one of the for loops and one of the $alpha arrays as well as the $k variable. If you wanted to add one to make it return all 4 letter long permutations, add another for loop, another $alpha array and the variable $m. This should be pretty easy to see if you study it long enough. Good luck with your PHP permutations, I hope this article helped!