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
 
open List;
 
val rec selfNumberNr = fn NR =>
let
  val rec sumdgt = fn 0 => 0 | n => Int.rem (n, 10) + sumdgt (Int.quot(n ,10));
  val rec isSelf  = fn ([],l1,l2) => []
   | (x::tt,l1,l2) => if exists (fn i=>i=x) l1 orelse exists (fn i=>i=x) l2
			  then ( isSelf (tt,l1,l2)) else x::isSelf (tt,l1,l2) ;
 
  val rec partcount =  fn  (n, listIn , count , selfs) =>
         if count >= NR then  nth (selfs, length selfs + NR - count -1)
           else
         let
          val range   = tabulate (81 , fn i => 81*n +i+1) ;
          val listOut = map (fn i => i + sumdgt i ) range ;
          val selfs   = isSelf (range,listIn,listOut)
         in
          partcount ( n+1 , listOut , count+length (selfs) , selfs )
       end;
in
  partcount (0,[],0,[])
end;
 
app  ((fn s => print (s ^ " ")) o Int.toString o selfNumberNr)  (tabulate (50,fn i=>i+1)) ;
selfNumberNr 100000000 ;
 