Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |||
|---|---|---|---|
|
Re: Puzzle Time
by LanX (Saint) on Dec 23, 2012 at 03:47 UTC | |||
(I suppose only positive numbers otherwise multiply it with 2) The following is just a variation of the code I recently used: (I love branch and bound! :) 9! = 362880 possible combinations¹ are checked in a recursive function. output Read more... (4 kB)
Cheers Rolf | [reply] [d/l] [select] | ||
by ikegami (Patriarch) on Dec 24, 2012 at 06:41 UTC | |||
with
to cut down the time taken to 2/3 (6s from 9s). Also, there's reason to use a hash (outside of testing). Full code:
| [reply] [d/l] [select] | ||
by Athanasius (Archbishop) on Dec 23, 2012 at 10:21 UTC | |||
++LanX for an excellent answer (way faster than my brute force approach). Allow me one nit-pick: 9! = 362880 possible combinations are checked in a recursive function. Throwing in a counter shows that sub append_digit is actually called 986,410 times. Took me a while to work out why... 9! is the number of combinations if every number has exactly 9 digits. But we also allow numbers with 1, 2, ..., 8 digits. So the total number of combinations is:
(That 1 extra sub call is the first one, when @number is empty.) :-) Read more... (1021 Bytes)
| [reply] [d/l] [select] | ||
by LanX (Saint) on Dec 23, 2012 at 15:25 UTC | |||
Yeah I noticed this, but was too tired to correct it. =) Anyway my guess was wrong (9!+8!+7!+...) you got it right. > (way faster than my brute force approach). If it's about speed you can limit the $maxlevel, because the longest number can't have more than 7 digits:
Even your approach with a brute force loop could compete when only considering 7 digits, cause you don't have the overhead of 1 million function calls.
Cheers Rolf ¹) and therefor a 7 digit number excludes 4 to be divisible by 9. | [reply] | ||
by Anonymous Monk on Dec 23, 2012 at 04:28 UTC | |||
| [reply] | ||
|
Re: Puzzle Time
by Athanasius (Archbishop) on Dec 23, 2012 at 03:39 UTC | |||
First, some assumptions: There are 9 different digits allowed, and any number having more than 9 digits will necessarily have some digits repeated. So, the upper bound is 987,654,321.
Hope that helps,
| [reply] [d/l] | ||
|
Re: Puzzle Time
by golux (Chaplain) on Dec 23, 2012 at 05:03 UTC | |||
| [reply] [d/l] | ||
|
Re: Puzzle Time
by BrowserUk (Patriarch) on Dec 23, 2012 at 03:59 UTC | |||
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] | ||
|
Re: Puzzle Time
by hdb (Monsignor) on Jun 25, 2013 at 14:58 UTC | |||
Here are the answers for the number representations with bases 2 to 9 using brute force and Math::BaseCalc.
| [reply] [d/l] | ||
|
Re: Puzzle Time
by Anonymous Monk on Dec 23, 2012 at 02:57 UTC | |||
| [reply] | ||
|
Re: Puzzle Time
by hdb (Monsignor) on Sep 20, 2013 at 08:02 UTC | |||
Just seen this related post: 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? which asks for the largest such number. | [reply] | ||