VMGuru.com - Home of the Virtualization Gurus

Our Books REALLY are Available Now!

Don’t become overwhelmed by your VMware Infrastructure project. Use the books that tens of thousands of others have used to design, plan, and implement their virtual infrastructures.Download Now!

Fun with PowerShell - Calculate Permutations PDF Print
Written by Scott Herold   
Saturday, 13 June 2009 22:17

I got sidetracked on Twitter tonight with a conversation between @jasonboche and @vmdoug, and since my wife is out of town visiting family I figured what better way to spend a Saturday night than to write a quick PowerShell script that can calculate the number of unique permutations based on 2 criteria.

First is the total number of available elements.  in this case, we were looking at the number of available URL combinations on bit.ly before they would "run out".  Without spending any more than 15 seconds investigating, I determined that bit.ly uses only alphanumeric keys, but is case sensitive.  That means we have 26 uppercase letters, 26 lowercase letters, and 10 integers (0-9).  That gives us 62 total elements.

The second criteria is the number of total elements used in the combination.  In this case, bit.ly still uses 5 character URL strings.

The math behind this is something that I actually still remembered from high school oh so many years ago and is P = n!/(n-r)! where P is the number of Permutations, n is the total number of available elements, and r is the number of elements used in the combination.

Borrowing a function from stackoverflow.com (The nested loop calculation was pissing me off so I had to cheat), I was able to put together the following PowerShell script that can rather quickly do basic permutation calculations.

function factorial( [int] $f ) 
{ 
$result = 1
if ( $f -gt 1){
$result = $f * ( factorial ( $f - 1 ) )  
}
$result
}
 
function permutation( [int] $n, [int] $r )
{
(factorial $n) / (factorial ($n - $r) )
}
[int] $n = Read-Host "Total number of available elements:"
[int] $r = Read-Host "Total number of elements in combination:"
 
if ($n -ge 1 -and ($n - $r) -ge 0) {
permutation $n $r
}
else{
Write-Host "Invalid input parameters"
} 

 

So, when all is said and done, by using this script we can determine that when using 62 total elements in 5 element combinations, bit.ly currently has 776,520,240 URL combinations available.  if they were to change to a 6 character URL, the number jumps to 44,261,653,680, in addition to the 776,520,240 combinations from their 5 character URLs.  I think its safe to say that as long as the bit.ly database can handle the load, we don't need to worry about them running out of URLs any time soon.

Trackback(0)
Comments (3)Add Comment
Arnim van Lieshout
Arnim van Lieshout
June 15, 2009
Votes: +1
...

FUN!
You are assuming that the URL must be a combination of 5 elements out of 62 elements.
But AFAIK it is allowed to use an element multiple times, so this would simplify the math to 62^5 which comes down to 916,132,832 different URLs

On the second hand, I've seen several 6 character URLs from bit.ly, so I guess their algorythm is somekind different.

But I loved the math fun.

-Arnim

Matt John
Matt John
September 26, 2009
Votes: +0
Re. Fun with PowerShell - Calculate Permutations

In this section only, the traditional definition from combinatorics is used: a permutation is an ordered sequence of elements selected from a given finite set, without repetitions, and not necessarily using all elements of the given set. For example, given the set of letters {C, E, G, I, N, R}, some permutations are ICE, RING, RICE, NICER, REIGN and CRINGE, but also RNCGI – the sequence need not spell out an existing word. ENGINE, on the other hand, is not a permutation, because it uses the elements E and N twice.

If n denotes the size of the set – the number of elements available for selection – and only permutations are considered that use all n elements, then the total number of possible permutations is equal to n!, where "!" is the factorial operator. This can be shown informally as follows. In constructing a permutation, there are n possible choices for the first element of the sequence. Once it has been chosen, n − 1 elements are left, so for the second element there are only n − 1 possible choices. For the first two elements together, that gives us

n × (n − 1) possible permutations.
For selecting the third element, there are then n − 2 elements left, giving, for the first three elements together,

n × (n − 1) × (n − 2) possible permutations.
Continuing in this way until there are only 2 elements left, there are 2 choices, giving for the number of possible permutations consisting of n − 1 elements:

n × (n − 1) × (n − 2) × ... × 2.
The last choice is now forced, as there is exactly one element left. In a formula, this is the number

n × (n − 1) × (n − 2) × ... × 2 × 1
(which is the same as before because the factor 1 does not make a difference). This number is, by definition, the same as n!.

In general the number of permutations is denoted by , , or sometimes , where:

n is the number of elements available for selection, and
r is the number of elements to be selected (0 ≤ r ≤ n).
For the case where r = n it has just been shown that P(n, r) = n!. The general case is given by the formula:

I am doing itil questions and in this course, there are a lot of question of permutation.

As before, this can be shown informally by considering the construction of an arbitrary permutation, but this time stopping when the length r has been reached. The construction proceeds initially as above, but stops at length r. The number of possible permutations that has then been reached is:

P(n, r) = n × (n − 1) × (n − 2) × ... × (n − r + 1).
So:

n! = n × (n − 1) × (n − 2) × ... × 2 × 1
= n × (n − 1) × (n − 2) × ... × (n − r + 1) × (n − r) × ... × 2 × 1
= P(n, r) × (n − r) × ... × 2 × 1
= P(n, r) × (n − r)!.
But if n! = P(n, r) × (n − r)!, then P(n, r) = n! / (n − r)!.

For example, if there is a total of 10 elements and we are selecting a sequence of three elements from this set, then the first selection is one from 10 elements, the next one from the remaining 9, and finally from the remaining 8, giving 10 × 9 × 8 = 720. In this case, n = 10 and r = 3. Using the formula to calculate P(10,3),


In the special case where n = r the formula above simplifies to:


The reason why 0! = 1 is that 0! is an empty product, which always equals 1.

In the example given in the header of this article, with 6 integers {1..6}, this would be: P(6,6) = 6! / (6−6)! = (1×2×3×4×5×6) / 0! = 720 / 1 = 720.

Since it may be impractical to calculate n! if the value of n is very large, a more efficient algorithm is to calculate:

P(n, r) = n × (n − 1) × (n − 2) × ... × (n − r + 1).
Other, older notations include nPr, Pn,r, or nPr. A common modern notation is (n)r which is called a falling factorial. However, the same notation is used for the rising factorial (also called Pochhammer symbol)

n(n + 1)(n + 2)⋯(n + r − 1)r.
With the rising factorial notation, the number of permutations is (n − r + 1)r.

Matt John
itil questions
September 26, 2009
Votes: +0
Re. Fun with PowerShell - Calculate Permutations

In this section only, the traditional definition from combinatorics is used: a permutation is an ordered sequence of elements selected from a given finite set, without repetitions, and not necessarily using all elements of the given set. For example, given the set of letters {C, E, G, I, N, R}, some permutations are ICE, RING, RICE, NICER, REIGN and CRINGE, but also RNCGI – the sequence need not spell out an existing word. ENGINE, on the other hand, is not a permutation, because it uses the elements E and N twice.

If n denotes the size of the set – the number of elements available for selection – and only permutations are considered that use all n elements, then the total number of possible permutations is equal to n!, where "!" is the factorial operator. This can be shown informally as follows. In constructing a permutation, there are n possible choices for the first element of the sequence. Once it has been chosen, n − 1 elements are left, so for the second element there are only n − 1 possible choices. For the first two elements together, that gives us

n × (n − 1) possible permutations.
For selecting the third element, there are then n − 2 elements left, giving, for the first three elements together,

n × (n − 1) × (n − 2) possible permutations.
Continuing in this way until there are only 2 elements left, there are 2 choices, giving for the number of possible permutations consisting of n − 1 elements:

n × (n − 1) × (n − 2) × ... × 2.
The last choice is now forced, as there is exactly one element left. In a formula, this is the number

n × (n − 1) × (n − 2) × ... × 2 × 1
(which is the same as before because the factor 1 does not make a difference). This number is, by definition, the same as n!.

In general the number of permutations is denoted by , , or sometimes , where:

n is the number of elements available for selection, and
r is the number of elements to be selected (0 ≤ r ≤ n).
For the case where r = n it has just been shown that P(n, r) = n!. The general case is given by the formula:


As before, this can be shown informally by considering the construction of an arbitrary permutation, but this time stopping when the length r has been reached. The construction proceeds initially as above, but stops at length r. The number of possible permutations that has then been reached is:

P(n, r) = n × (n − 1) × (n − 2) × ... × (n − r + 1).
So:

n! = n × (n − 1) × (n − 2) × ... × 2 × 1
= n × (n − 1) × (n − 2) × ... × (n − r + 1) × (n − r) × ... × 2 × 1
= P(n, r) × (n − r) × ... × 2 × 1
= P(n, r) × (n − r)!.
But if n! = P(n, r) × (n − r)!, then P(n, r) = n! / (n − r)!.

For example, if there is a total of 10 elements and we are selecting a sequence of three elements from this set, then the first selection is one from 10 elements, the next one from the remaining 9, and finally from the remaining 8, giving 10 × 9 × 8 = 720. In this case, n = 10 and r = 3. Using the formula to calculate P(10,3),


In the special case where n = r the formula above simplifies to:


The reason why 0! = 1 is that 0! is an empty product, which always equals 1.

In the example given in the header of this article, with 6 integers {1..6}, this would be: P(6,6) = 6! / (6−6)! = (1×2×3×4×5×6) / 0! = 720 / 1 = 720.

Since it may be impractical to calculate n! if the value of n is very large, a more efficient algorithm is to calculate:

P(n, r) = n × (n − 1) × (n − 2) × ... × (n − r + 1).
Other, older notations include nPr, Pn,r, or nPr. A common modern notation is (n)r which is called a falling factorial. However, the same notation is used for the rising factorial (also called Pochhammer symbol)

n(n + 1)(n + 2)⋯(n + r − 1)r.
With the rising factorial notation, the number of permutations is (n − r + 1)r.

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy
 

Buy on Amazon