Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to 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?

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@/
  • Comment on 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?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: 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 14:31 UTC

    I must say, I'm a bit surprised by the times other people are getting - as written above, my program takes about 1 second on my relatively underpowered laptop. (1.3 Ghz Pentium something, 512 MB ram)

    Even if you simplify my program down by removing both "elsif" blocks you're only up to about 2.5 seconds. Removing the "if ($ndig == 1)" block does bring you up to 34 seconds.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://503333]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2024-04-24 14:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found