Python Forum
Calculating Pandigital Numbers
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculating Pandigital Numbers
#1
Inspired by a passage in the book Things to Make and Do in the Fourth Dimension, I threw together an algorithm to find all existing pandigital numbers in a given base. It uses a brute force method utilizing the itertools.permutations function, checking every possible combination of digits. This works well enough for bases up to 11, but beyond that the calculation time goes out of control, as the time function increases with the factorial of one minus the input base. A quick calculation tells me completion of my project (through base 64) would take over 6x10^73 years. Personally, I plan to have moved on to other projects by then.

Does anyone have any more efficient method of testing for pandigital numbers?
Reply
#2
Doesn't sound like something that we all do every day.
Reply
#3
(Jan-27-2017, 08:43 PM)Flexico Wrote: Inspired by a passage in the book Things to Make and Do in the Fourth Dimension, I threw together an algorithm to find all existing pandigital numbers in a given base. It uses a brute force method utilizing the itertools.permutations function, checking every possible combination of digits. This works well enough for bases up to 11, but beyond that the calculation time goes out of control, as the time function increases with the factorial of one minus the input base. A quick calculation tells me completion of my project (through base 64) would take over 6x10^73 years. Personally, I plan to have moved on to other projects by then.

Does anyone have any more efficient method of testing for pandigital numbers?

The strict definition (all digits used at least once) means that there is an infinite number of pandigital numbers in any given base.

A more restrictive definition (all digits used exactly once, and no zero on left) still produces (n-1)! numbers, but you don't need to "test" them, just generate them, and yes, 63! is a big number...
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
(Jan-27-2017, 11:43 PM)Ofnuts Wrote:
(Jan-27-2017, 08:43 PM)Flexico Wrote: Inspired by a passage in the book Things to Make and Do in the Fourth Dimension, I threw together an algorithm to find all existing pandigital numbers in a given base. It uses a brute force method utilizing the itertools.permutations function, checking every possible combination of digits. This works well enough for bases up to 11, but beyond that the calculation time goes out of control, as the time function increases with the factorial of one minus the input base. A quick calculation tells me completion of my project (through base 64) would take over 6x10^73 years. Personally, I plan to have moved on to other projects by then.

Does anyone have any more efficient method of testing for pandigital numbers?

The strict definition (all digits used at least once) means that there is an infinite number of pandigital numbers in any given base.

A more restrictive definition (all digits used exactly once, and no zero on left) still produces (n-1)! numbers, but you don't need to "test" them, just generate them, and yes, 63! is a big number...

Yes, I'm using the "every number used exactly once with no zeros" definition. And generating all the possibilities is pretty straightforward (if time-consuming), but how do I know which ones are pandigital?

Oh wait -- I should specify. I'm searching for the ones that follow the rule of the first digit being divisible by 1, the first two digits being divisible by 2, etc. I forgot that's not included in the definition for "pandigital" heh. The only one in base 10 is 381,654,729 for example. That's what I meant by testing.
Reply
#5
(Jan-28-2017, 12:59 AM)Flexico Wrote: I'm searching for the ones that follow the rule of the first digit being divisible by 1, the first two digits being divisible by 2, etc. I forgot that's not included in the definition for "pandigital" heh. The only one in base 10 is 381,654,729 for example.
Do you mean "the second digit being divisible by 2"? Or are you looking at the product? Your example isn't great here.

Have you tried to write a function to test this? Is it non-functional, or simply inefficient? In either case, what example(s) demonstrate the problem?
Reply
#6
(Jan-28-2017, 01:08 AM)micseydel Wrote:
(Jan-28-2017, 12:59 AM)Flexico Wrote: I'm searching for the ones that follow the rule of the first digit being divisible by 1, the first two digits being divisible by 2, etc. I forgot that's not included in the definition for "pandigital" heh. The only one in base 10 is 381,654,729 for example.
Do you mean "the second digit being divisible by 2"? Or are you looking at the product? Your example isn't great here.

Have you tried to write a function to test this? Is it non-functional, or simply inefficient? In either case, what example(s) demonstrate the problem?

My apologies, the source made it sound like this was a well-known puzzle. 3 is divisible by 1, 38 is divisible by 2, 381 is divisible by 3, 3816 is divisible by 4, etc. It works for the whole number.

My function is perfectly working and reliable; the problem is when I get into bases higher than 12, it takes many days to compute, and when I get into even higher bases we're talking on the scale of the birth and death of galaxies. XD

I attached my code for you to test if you like. The "flex" library is just a bunch of functions I wrote that I use in many programs, such as my base conversion function.

Attached Files

.zip   Pandig_Scripts.zip (Size: 22.17 KB / Downloads: 262)
Reply
#7
(Jan-28-2017, 12:59 AM)Flexico Wrote: Oh wait -- I should specify. I'm searching for the ones that follow the rule of the first digit being divisible by 1, the first two digits being divisible by 2, etc. I forgot that's not included in the definition for "pandigital" heh. The only one in base 10 is 381,654,729 for example. That's what I meant by testing.

You don't need to "test" either. These are fairly restrictive conditions... For instance in base 10 the 2nd digit must be [0,2,4,6,8], the 5th digit must be 0 or 5... Once you have defined the first two digits, there are only 3 possibilities for the 3rd digit... So you don't need to generate them all. So you end up traveling a tree where each branch is a digit in the final number, but you will find that most branches are dead-ends.

But of course the divisibility criteria are different in each numeral base (the only common one, divisibility by N-1, is unfortunately always true for your pandigital numbers). But you can also compute the next possible digits by brute force, this would still be a lot faster than generating them all.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,725 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020