LOOP: for (my $i = 98765432; $i > 0; $i--) { next if $i =~ /0/ or $i =~ /(\d)\d*\1/; $i % $_ and next LOOP for split '', $i; print "$i\n"; exit; }