Numbers do not feel. Do not bleed or weep or hope. They do not know bravery or sacrifice. Love and allegiance. At the very apex of callousness, you will find only ones and zeros.
Amie Kaufman, Illuminae

A pandigital number is a number that contains each digit starting from one, up to the total length of the number. For instance, 15342 is a pandigital number, since it has all the digits starting from zero up to the total length of the number, which is 5. On the contrary, 4433 is not a pandigital number, since it does not have 1 and 2 among its digits.
Bear in mind that some definitions of pandigital numbers start from 0 instead of 1. The functions that we consider here take care of the definition given in the above paragraph, but they are easily modifiable to be implementable by the other definition as well.
Three methods are offered to check if a given number is in fact pandigital or not. They are basically very similar to each other, and only differ in their performance and efficiency.
Method 1
[pastacode lang=”python” manual=”%23The%20first%20function%0Adef%20pandigital1(n)%3A%0A%20%20%20%20%0A%20%20%20%20%23Turn%20the%20number%20into%20string%0A%20%20%20%20number%20%3D%20str(n)%0A%20%20%20%20%0A%20%20%20%20%23Empty%20list%20to%20store%20the%20digits%20of%20the%20number%0A%20%20%20%20digits%20%3D%20%5B%5D%0A%20%20%20%20%0A%20%20%20%20%23Go%20through%20each%20digit%20of%20the%20number%20and%20check%20if%20it%20is%20repetitive%20or%20not%0A%20%20%20%20for%20digit%20in%20number%3A%0A%20%20%20%20%20%20%20%20%23If%20the%20digit%20already%20exists%20in%20the%20digits%20list%2C%20then%20this%20number%20is%20not%20pandigital%0A%20%20%20%20%20%20%20%20if%20digit%20in%20digits%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20False%0A%20%20%20%20%20%20%20%20%23Otherwise%2C%20add%20the%20digit%20to%20the%20digits%20list%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20digits.append(digit)%0A%20%20%20%20%0A%20%20%20%20%23What%20we%20expect%2C%20by%20the%20definition%20of%20a%20pandigit%20number%20%20%20%20%20%20%20%20%0A%20%20%20%20goal%20%3D%20%5Bstr(x)%20for%20x%20in%20range(1%2C%20len(number)%2B1)%5D%20%23%20goal%20%3D%20%5B1%2C%202%2C%20….%2C%20len(number)%5D%0A%20%20%20%20%0A%20%20%20%20%23If%20the%20sorted%20list%20of%20digits%20is%20equal%20to%20what%20we%20want%2C%20then%20it%20is%20a%20pandigital%20number%0A%20%20%20%20if%20goal%20%3D%3D%20sorted(digits)%3A%0A%20%20%20%20%20%20%20%20return%20True%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20False” message=”” highlight=”” provider=”manual”/]
- In this algorithm, we first take the number and turn it into string.
- Then we loop through the elements of this string, and if these elements are not repetitive, we add them to a pre-defined empty list
- We also define what we expect from a pandigital number. This step is the same in all of these methods. So, for example, if we have a 5-digit number, we expect that its sorted digits are of the form: [1, 2, 3, 4, 5]. Starting from 1 and ending at the length of the number, which is 5 here.
- At a final step, we check if these two lists are the same: If so, then it is a pandigital number.
Method 2
[pastacode lang=”python” manual=”%23The%20second%20function%0Adef%20pandigital2(number)%3A%0A%20%20%20%20%22%22%22This%20function%20checks%20if%20a%20number%20can%20be%20written%20as%20a%201%20through%209%20pandigital.%22%22%22%0A%20%20%20%20%0A%20%20%20%20%23I%20begin%20by%20changing%20the%20data%20type%20to%20string%0A%20%20%20%20number%20%3D%20str(number)%0A%20%20%20%20%23Then%20I%20sort%20the%20digits%20in%20the%20number%0A%20%20%20%20number%20%3D%20sorted(number)%0A%20%20%20%20%0A%20%20%20%20%23Now%20I%20create%20a%20list%20of%20the%20digits%20starting%20from%201%20until%20the%20length%20of%20the%20given%20number%0A%20%20%20%20digits%20%3D%20%5Bstr(x)%20for%20x%20in%20range(1%2C%20len(number)%2B1)%5D%0A%20%20%20%20%0A%20%20%20%20%23Now%20I%20check%20if%20the%20two%20are%20the%20same%0A%20%20%20%20if%20number%20%3D%3D%20digits%3A%0A%20%20%20%20%20%20%20%20return%20True%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20False” message=”” highlight=”” provider=”manual”/]
- Here again, we start by receiving the number and turning it into the string.
- But we do not loop through each and every digit again, rather we just sort the string that we obtained in the previous step.
The last two steps are exactly the same as the previous method.
Method 3
[pastacode lang=”python” manual=”%23The%20third%20function%0Adef%20pandigital3(n)%3A%0A%20%20%20%20%23Turn%20the%20number%20into%20a%20string%0A%20%20%20%20number%20%3D%20str(n)%0A%20%20%20%20%23What%20we%20expect%2C%20by%20the%20definition%20of%20a%20pandigit%20number%20%0A%20%20%20%20digits%20%3D%20%5Bstr(x)%20for%20x%20in%20range(1%2C%20len(number)%2B1)%5D%0A%20%20%20%20%23Check%20if%20the%20two%20are%20the%20same%0A%20%20%20%20return%20set(number)%20%3D%3D%20set(digits)” message=”” highlight=”” provider=”manual”/]
This one is very similar to the second method. Instead of sorting the number, we just turn it into a set. A set cannot have repetitive numbers, so it helps shorten the code.
Comparison
Let’s check how these functions can be ranked based on performance and efficiency. I guess it is going to be a tight match.
I check how many numbers each of these function find for numbers below 1,000,000, so that we can be sure that the functions are working properly. And I also check how much does it take for each function to do the job.
Here is the result:
As it turns out, all the three functions are working properly as they all found 873 pandigital numbers below 1,000,000. Nevertheless, the first function is the fastest of them all, needing about half the time that the two other function need to process all the data.