XP is just a number | |
PerlMonks |
Re: Puzzle: What is the largest integer whose digits are all different (and do not include 0) that is divisible by each of its individual digits?by fizbin (Chaplain) |
on Oct 27, 2005 at 13:57 UTC ( [id://503333]=note: print w/replies, xml ) | Need Help?? |
You appear to be making some of the mistakes the author said you needed to avoid - namely, you aren't really cutting down your search space. You're still performing all these checks on every single number; it would be better to only form the numbers that pass the checks mentioned in comments at the top, rather than to form all numbers and then reject those that fail. Not only that, but you're doing some stuff that doesn't make sense - you could replace your check code with this routine and it would still accept/reject the same numbers but would be tons faster:
The check about 5 and even numbers is redundant by the point you do it, since if it has 5 and an even number then it either has a 0 or will be rejected by the divisibility test. So what you need to do is cut down on the number of possibilities to begin with. Say, something like this:
Note that this code won't print the answers out in numerical order. You'll have to sort them with something else. Update: Got rid of some extraneous code that doesn't add much since the answers don't get generated in numerical order anyway.
--
In Section
Seekers of Perl Wisdom
|
|