Sum of the digits of n is substring of n: Difference between revisions
Content deleted Content added
→{{header|Haskell}}: Added a Haskell example |
|||
Line 85:
48 such numbers found.
</pre>
=={{header|Haskell}}==
<lang haskell>import Data.Char (digitToInt)
import Data.List (isInfixOf)
import Data.List.Split (chunksOf)
-------- SUM OF THE DIGITS OF N IS A SUBSTRING OF N ------
digitSumIsSubString :: String -> Bool
digitSumIsSubString =
isInfixOf
=<< show . foldr ((+) . digitToInt) 0
--------------------------- TEST -------------------------
main :: IO ()
main = do
let limit = 999
xs =
filter digitSumIsSubString $
fmap show [0 .. limit]
w = length (last xs)
putStrLn $
show (length xs)
<> " matches in [0.."
<> show limit
<> "]\n"
putStrLn $
unlines
(unwords <$> chunksOf 10 (justifyRight w ' ' <$> xs))
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)
</lang>
{{Out}}
<pre>48 matches in [0..999]
0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100
109 119 129 139 149 159 169 179 189 199
200 300 400 500 600 700 800 900 910 911
912 913 914 915 916 917 918 919</pre>
=={{header|Julia}}==
|