Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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 McDarren (Abbot)
on Oct 27, 2005 at 14:07 UTC ( [id://503342]=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?

This looks like fun, so I've had a go :)
#!/usr/bin/perl -w use strict; my $biggest_number = 987654321; BIGNUMBER: while ($biggest_number) { # Discard all odd numbers (not divisible by 2) if ($biggest_number % 2) { $biggest_number--; next BIGNUMBER; } my @divisors = split //, $biggest_number; # Discard all numbers containing zero if (grep {$_ eq "0"} @divisors) { $biggest_number--; next BIGNUMBE +R; } # Discard all numbers with repeated digits my %seen = (); for (@divisors) { if ($seen{$_}++) { $biggest_number--; next BIGNUMBER; } } # Get this far, give it a try print "Trying $biggest_number..."; DIVISORS: for (@divisors) { if ($biggest_number % $_) { $biggest_number--; print "Failed!\n"; next BIGNUMBER; } } print "Success!!\n"; for (@divisors) { print "$biggest_number / $_ = ", $biggest_number / $_, "\n"; } exit; }
Update:Well, I'd now like to stake my claim as having come up with the slowest solution that gets the correct answer, and here is my evidence:
Trying 9867312...Success!! 9867312 / 9 = 1096368 9867312 / 8 = 1233414 9867312 / 6 = 1644552 9867312 / 7 = 1409616 9867312 / 3 = 3289104 9867312 / 1 = 9867312 9867312 / 2 = 4933656 real 384m53.120s user 382m59.960s sys 0m14.600s
Almost 6.5 hours, I imagine that would have been quite impressive in the late 50's :D
  • 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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found