Two consecutive numbers both have a digital sum which is a multiple of 11. What is the smallest pair of numbers?
Note: the digital sum is the number equalling the sum of all the digits of the number. For example the digital sum of 123456 is 21.
Perl solution:
my $prev_sum=2; # any non-zero value will do
my $sum;
for (29..1e7) { # smallest to relatively large
$sum=0;
map {$sum+=$_;} split("", $_); # calculate sum
unless ($sum % 11) { # if sum divisible by 11
unless ($prev_sum % 11) { # same for previous
die "$_ and $_ - 1\n" # end search
} else {
$prev_sum=$sum
}
} else {
$prev_sum=2; # not divisible by 11
}
}
|
Posted by Moira
on 2009-01-21 22:58:24 |