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
 
def sumdigits: tostring | explode | map([.]|implode|tonumber) | add;
 
def gsum: . + sumdigits;
 
def isnonself:
  def ndigits: tostring|length;
  . as $i
  | ($i - ($i|ndigits)*9) as $n
  | any( range($i-1; [0,$n]|max; -1);
          gsum == $i);
 
# an array
def last81:
  [limit(81; range(1; infinite) | select(isnonself))];
 
# output an unbounded stream
def selfnumbers:
  foreach range(1; infinite) as $i ( [0, last81];
    .[0] = false
    | .[1] as $last81
    | if $last81 | bsearch($i) < 0
      then .[0] = $i
      | if $i > $last81[-1] then .[1] = $last81[1:] + [$i | gsum ] else . end
      else .
      end;
      .[0] | select(.) );
 
 
"The first 50 self numbers are:", last81[:50],
"",
(nth(100000000 - 1;  selfnumbers)
 | if . == 1022727208 
   then "Yes, \(.) is the 100,000,000th self number." 
   else "No,  \(.i) is actually the 100,000,000th self number."
   end)