#What is the largest integer whose digits are all different (and do not include 0) that is divisible by each of its individual digits? #Requirements: # -- Integer # -- Digits all different (largest possible would be 987654321) # -- Divisible by each of individual digits # note: # this integer cannot contain the digit 5 if it also contains any even digit, # because then the number would be divisible by 10, and the last digit would be 0 # similarly, if the last digit is odd, it can't contain any other even digit. use strict; use warnings; my $max_number = 987654321; #my $max_number = 10000; foreach ( 0 .. $max_number - 1 ) { my $test_number = $max_number - $_; print "number / 100000: " . $test_number/100000 . "\n" if $test_number % 100000 == 0; if ( passes($test_number) ) { print "$test_number passes!\n"; die; } } sub passes { my $test_number = shift() || die "no test number"; my @digits = split("", $test_number); my %seen; my $last_digit; #fail if see the same digit twice. foreach ( @digits ) { return 0 if $_ == 0; #fail if contains 0 return 0 if $seen{$_}; $seen{$_} = 1; } #set the last digit $last_digit = $digits[$#digits]; #fail if contains 5, and any other digit is even #or the last digit is 1, and any other digit is even if ( $seen{5} || $last_digit % 2 == 1 ) { for ( qw(2 4 6 8) ) { return 0 if $seen{$_}; } } for ( keys %seen ) { return 0 unless $test_number % $_ == 0 ; } return 1; }