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?
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?

I'd be very surprised if that is the largest. I'm sure it is among the longest. But I'd bet money that the largest starts with a 9, and I'd bet a lot of money that it doesn't start with a 1. Update: I didn't notice it was only 7 digits until I wrote a short script that showed that there is no 9-digit answer, which surprised me -- but it's too early for clear thinking ATM :). I should have noted that I looked at the code and it doesn't appear to look for the largest answer, just the first answer that it finds (and it doesn't look at the possible answers from largest to smallest).

Update2: And here is the quick hack showing the first block that failed to find any 9-digit answer and then an added block to find the answer. Note that this code searches the potential solutions in order from largest to smallest so it can stop as soon as one answer is found. It isn't particularly fast to run (taking 14 seconds), but it was very fast to write. (:

use Algorithm::Loops qw( NextPermuteNum ); my @digs= (1..9); my @map; @map[1..9]= reverse 1..9; do { my $num= join '', @map[ @digs ]; for( 9, 8, 7, 5, 0 ) { die "$num\n" if ! $_; last if 0 != $num % $_; } } while( NextPermuteNum(@digs) ); my $prev= 0; for my $len ( reverse 1..8 ) { do { my $num= substr( join( '', @map[ @digs ] ), 0, $len ); if( $num != $prev ) { for( $num =~ /./g, 0 ) { die "$num\n" if ! $_; last if 0 != $num % $_; } } $prev= $num; } while( NextPermuteNum(@digs) ); }

- tye        

Replies are listed 'Best First'.
Re^3: Puzzle: What is the largest integer ... (not "a longest")
by Limbic~Region (Chancellor) on Oct 27, 2005 at 14:14 UTC
    tye,
    I borrowed Algorithm::Loops for the following brute-force approach:
    #!/usr/bin/perl use strict; use warnings; use Algorithm::Loops qw/NestedLoops NextPermute/; my $max = 1; my $next = GenPowerSet(9); while ( my @list = $next->() ) { PERMUTE: while ( NextPermute(@list) ) { my $num = join '', @list; next PERMUTE if $num < $max; for ( @list ) { next PERMUTE if $num % $_; } $max = $num if $num > $max; } } print $max; sub GenPowerSet { my $end = shift; return NestedLoops( [ [ 1..$end ], ( sub { [ $_+1 .. $end ] } ) x $end, ], { OnlyWhen => 1, }, ); }
    It came up with 9_867_312 in about 30 seconds.

    Cheers - L~R

Re^3: Puzzle: What is the largest integer ... (not "a longest")
by fizbin (Chaplain) on Oct 27, 2005 at 14:59 UTC

    Note that you can cut the time in half or there abouts by adding just two simple checks; this version of your program runs in 6.1 seconds on my machine - the original without the initial 9-digit loop takes 13 seconds on my machine:

    use Algorithm::Loops qw( NextPermuteNum ); my @digs= (1..9); my @map; @map[1..9]= reverse 1..9; my $prev= 0; for my $len ( reverse 1..8 ) { do { my $num= substr( join( '', @map[ @digs ] ), 0, $len ); if( $num != $prev and $num !~ /[2468].*[13579]$/ and $num !~ / +5./) { for( $num =~ /./g, 0 ) { die "$num\n" if ! $_; last if 0 != $num % $_; } } $prev= $num; } while( NextPermuteNum(@digs) ); }

    Note that all I've done is add two regex tests - that you don't have any even digits if the last digit is odd, and that you never have a 5 in any but the last position.

    Reducing the search space is almost always a good idea.

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

      You can make it even faster by just dropping 5 from the list, since including a 5 excludes all even digits and restricts the solution length to 5 digits (which we quickly prove is too short). You can also make it faster by more efficiently skipping the redundant permutations. That's the nature of quick hacks. (:

      - tye        

Re^3: Puzzle: What is the largest integer ... (not "a longest")
by Skeeve (Parson) on Oct 27, 2005 at 14:02 UTC

    If it's the longest it's also the largest given the fact that (leading) zeroes are not allowed.

    OTOH: It's neither ;-)


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e