in reply to Re^2: Puzzle: What is the largest integer ... (not "a longest")
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?
It came up with 9_867_312 in about 30 seconds.#!/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, }, ); }
Cheers - L~R
|
---|