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
 
load "stdlib.ring"
 
see "working..." + nl
see "The first 50 self numbers are:" + nl
 
n = 0
num = 0
limit = 51
limit2 = 10000000
 
while true
    n = n + 1
    for m = 1 to n
        flag = 1
        sum = 0
        strnum = string(m)
        for p = 1 to len(strnum)
            sum = sum + number(strnum[p])
        next
        sum2 = m + sum
        if sum2 = n
           flag = 0
           exit
        else
           flag = 1
        ok
     next
     if flag = 1
        num = num + 1
        if num < limit
           see "" + num + ". " + n + nl
        ok
        if num = limit2
           see "The " + limit2 + "th self number is: " + n + nl
        ok
        if num > limit2
           exit
        ok
     ok
end
 
see "done..." + nl
 