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?
Some minor changes to your code results in 9867312. Took my P4 3.2 w/4GB about 9.3 seconds to run it.
use strict; use warnings; use Algorithm::Permute qw( permute ); use Math::Combinatorics qw( combine ); my @arr = reverse 1 .. 9; my $m = 0; foreach my $n ( @arr ) { my $last = 0; foreach my $arr ( combine( $n, @arr ) ) { my @v = @$arr; permute { my $v = divisor(@v); if ( (my $w = join('',@v)) % $v == 0 ) { $m = $w unless $m > $w; #print "$w\n"; $last = 1; } } @v; } last if $last; } print "$m\n"; sub divisor { my $d = 1; for (reverse sort @_) { $d *= $_ unless $d % $_ == 0 } $d; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: 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 dragonchild (Archbishop) on Oct 27, 2005 at 16:42 UTC |