Factorial Permutations Using Python

ofer shmueli
3 min readApr 7, 2021

We use the factorial function to multiply the whole number from a chosen number down to one.

So for example, 4 factorial (4!) is 4 times 3 times2 times 1, which is equal to 24.

6 factorial (6!) is 6 times 5 times 4 times 3 times 2, times 1, and that is equal to 720.

When do we use Factorial?

We use the factorial function for different reasons. One of them is to count how many ways we can arrange different objects. Those objects can be cards, those objects can be letters, those objects can be people, they can be anything.

If we take for example the word love, which has 4 characters, we can reorder the word objects, the characters in 24 ways ( 4!)

Our Script

So let’s look at a basic Python script where we enter our word. And our script will tell us according to the word length, how many combinations can be achieved.

Let’s start by importing the math model.

And the next thing that we will do is the user input that asks “what is the word” for which you want to calculate the number of permutations?

The next thing that we will do is to add up another variable (num_word) which will actually hold the message (or the word) and calculate the length of it.

Next, we will use the factorial function.

We will create a variable, name it facto which will be equal to math. factorial, and we will enter num_word which is our variable that holds the number of objects in the word itself.

And the last thing to do is to print the number of the reordered objects ( letters) is and we will add up the factor which is our variable.

And let’s run this script. What is the word? Let’s start with the word attack, which actually has six objects, six letters, and the number of permutations or the number of reorder letters can be 720.

Another example. Let’s use the word information. The word information has 11 letters. And that sums up to almost 40 million. So let’s check it out in another collator, and let’s enter 11, which is our n. And let’s calculate the factorial and that also sums up to 40 million possibilities.

My book, where you can also Learn how Factorial function is used in cryptography

https://www.amazon.com/Book-Secrets-Cryptography-Handbook-Beginners-ebook/dp/B08YDDDZ24/ref=sr_1_1?dchild=1&keywords=book+of+secrets+cryptography&qid=1617723018&sr=8-1

--

--