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:

sub passes { my $test_number = shift() || die "no test number"; return 0 if ($test_number =~ /0/); # no zero return 0 if ($test_number =~ /(.).*\1/); # no dups for ( split(//,$test_number) ) { return 0 unless $test_number % $_ == 0 ; } return 1; }

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:

use strict; sub pass_divcheck { # dups and 0s are assumed already handled my ($test_number) = @_; for ( split(//,$test_number) ) { return 0 unless $test_number % $_ == 0 ; } return 1; } sub form_number { my ($num, @remaining_digits) = @_; print "$num\n" if pass_divcheck($num); # When dealing with the three low-order digits, we can eliminate som +e # possibilities. E.g. Any number divisible by 4 must end in a 2-dig +it # number divisible by 4. my $ndig = length($num); if ($ndig == 1) { if ($num % 2 != 0) { @remaining_digits = grep($_ % 2, @remaining_digits); } # After we have one digit, either 5 is already used or we # can't use 5 later anyway @remaining_digits = grep($_ % 5, @remaining_digits); } elsif ($ndig == 2) { if ($num % 4 != 0) { @remaining_digits = grep($_ % 4, @remaining_digits); return if ($num =~ /[48]/); } } elsif ($ndig == 3) { if ($num % 8 != 0) { @remaining_digits = grep($_ % 8, @remaining_digits); } } my $maxp = $#remaining_digits; foreach my $p (0..$maxp) { form_number($remaining_digits[$p] . $num, @remaining_digits[0..$p-1], @remaining_digits[$p+1..$m +axp]); } } form_number('', (1..9));

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.

--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

In reply to 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
in thread 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 tphyahoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.