The number can not have a zero in it, that implies that it can not have a 5 either since if it has a 5, it must be divisible by 5, but the only numbers divisible by 5 end in 5 or 0. It can't be zero, and if it is odd, it can't be divisible by 2, 4, 6 or 8. So that leaves 98764321 as possible digits the number can contain. The sum of those 8 digits is not divisible by three so the largest possible integer must use no more than 7 of them (since 3, 6 and 9 would be eliminated). Strictly by removing possibilities that cannot possibly work we are down to at most 7 digits.
We can deduce that the digit that won't get used is one of 1, 4, or 7 since those are the only ones where the removal will yield a sum divisible by 3. It is extremely unlikely be 1, since EVERY number is divisible by 1. Removing it reduces the number of digits available but doesn't gain anything as far as divisibility. It is unlikely to be 7 since 7 is prime and can't be made up of multiples of other numbers. Practically though, the code to accommodate these observations is longer running and more complex than just brute-forcing it from here.
In order to accommodate the most possible digits, the number must be divisible by 7, 8 and 9. If that is true then it is automatically divisible by 2, 3, 4, & 6 as they can all be made from the combinations of multiples of 2 and 3 which are present in 8 & 9; so we'll only bother to check multiples of 9 * 8 * 7 or 504.
All these optimizations get the run time to well under 1 second.
my $magic-number = 9 * 8 * 7; # 504my $div = 9876432 div $magic-number * $magic-number; # largest 7 digit multiple of 504 < 9876432for $div, { $_ - $magic-number } ... * -> $test { # only generate multiples of 504nextif $test ~~ / <[05]> /; # skip numbers containing 0 or 5nextif $test ~~ / (.).*$0 /; # skip numbers with non unique digitssay"Found $test"; # Found a solution, display itfor $test.comb {printf"%s / %s = %s\n", $test, $_, $test / $_; }last}
There are fewer analytical optimizations available for base 16. Other than 0, no digits can be ruled out so a much larger space must be searched. We'll start at the largest possible permutation (FEDCBA987654321) and work down so as soon as we find a solution, we know it is the solution. The combination of .race with .first lets us utilize concurrency and exit early when the single desired solution is found.
my $hex = 'FEDCBA987654321'; # largest possible hex numbermy $magic-number = [lcm] 1 .. 15; # find least common multiplemy $div = :16($hex) div $magic-number * $magic-number;# hunt for target stepping backwards in multiples of the lcmmy $target = ($div, * - $magic-number ... 0).race.first: -> \test {my \num= test.base(16); (num.contains('0') || num.comb.Bag.values.max > 1) ?? False !! True};my $hexnum = $target.base(16);say"Found $hexnum"; # Found a solution, display itsay' 'x12, 'In base 16', ' 'x36, 'In base 10';for $hexnum.comb {printf"%s / %s = %s | %d / %2d = %19d\n", $hexnum, $_, ($target / :16($_)).base(16), $target, :16($_), $target / :16($_);}