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
import Control.Monad (forM_)
import Text.Printf
 
selfs :: [Integer]
selfs = sieve (sumFs [0..]) [0..]
  where
    sumFs = zipWith (+) [ a+b+c+d+e+f+g+h+i+j
                        | a <- [0..9] , b <- [0..9]
                        , c <- [0..9] , d <- [0..9]
                        , e <- [0..9] , f <- [0..9]
                        , g <- [0..9] , h <- [0..9]
                        , i <- [0..9] , j <- [0..9] ]
 
    -- More idiomatic list generator is about three times slower
    --  sumFs = zipWith (+) $ sum <$> replicateM  10 [0..9]
 
    sieve (f:fs) (n:ns)
      | n > f = sieve fs (n:ns)
      | n `notElem` take 81 (f:fs) = n : sieve (f:fs) ns
      | otherwise = sieve (f:fs) ns
 
main = do
  print $ take 50 selfs
  forM_ [1..8] $ \i -> 
    printf "1e%v\t%v\n" (i :: Int) (selfs !! (10^i-1))