A number n is a self number if there is no number g such that g + the sum of g's digits = n. So 18 is not a self number because 9+9=18, 43 is not a self number because 35+5+3=43.


The task is:

 Display the first 50 self numbers;
 I believe that the 100000000th self number is 1022727208. You should either confirm or dispute my conjecture.

224036583-1 is a Mersenne prime, claimed to also be a self number. Extra credit to anyone proving it.


See also
 
OEIS: A003052 - Self numbers or Colombian numbers
Wikipedia: Self numbers
use strict;
use warnings;
use feature 'say';
use List::Util qw(max sum);
 
my ($i, $pow, $digits, $offset, $lastSelf, @selfs)
 = ( 1,   10,       1,       9,         0,       );
 
my $final = 50;
 
while () {
   my $isSelf = 1;
   my $sum = my $start = sum split //, max(($i-$offset), 0);
   for ( my $j = $start; $j < $i; $j++ ) {
      if ($j+$sum == $i) { $isSelf = 0 ; last }
      ($j+1)%10 != 0 ? $sum++ : ( $sum = sum split '', ($j+1) );
   }
 
   if ($isSelf) {
      push @selfs, $lastSelf = $i;
      last if @selfs == $final;
   }
 
   next unless ++$i % $pow == 0;
   $pow *= 10;
   $offset = 9 * $digits++
}
 
say "The first 50 self numbers are:\n" . join ' ', @selfs;