Numbers divisible by their individual digits, but not by the product of their digits.: Difference between revisions

→‎{{header|Haskell}}: Added a variant obtaining digit lists numerically.
m (→‎{{header|Haskell}}: Applied Hlint, Ormolu, pruned one import.)
(→‎{{header|Haskell}}: Added a variant obtaining digit lists numerically.)
Line 507:
848 864 888 936 999
</pre>
 
and another approach might be to obtain (unordered) digit lists numerically, rather by string conversion.
 
<lang haskell>import Data.List (unfoldr)
import Data.List.Split (chunksOf)
import Data.Tuple (swap)
 
-- DIVISIBLE BY ALL DIGITS, BUT NOT BY PRODUCT OF ALL DIGITS
 
p :: Int -> Bool
p n =
( ( (&&)
. all
( (&&) . (0 /=)
<*> (0 ==) . rem n
)
)
<*> (0 /=) . rem n . product
)
$ digits n
 
digits :: Int -> [Int]
digits = unfoldr go
where
go x
| 0 < x = Just $ swap $ quotRem x 10
| otherwise = Nothing
 
--------------------------- TEST -------------------------
main :: IO ()
main =
let xs = [1 .. 1000] >>= (\n -> [show n | p n])
w = length $ last xs
in (putStrLn . unlines) $
unwords
<$> chunksOf
10
(fmap (justifyRight w ' ') xs)
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</lang>
{{Out}}
<pre> 22 33 44 48 55 66 77 88 99 122
124 126 155 162 168 184 222 244 248 264
288 324 333 336 366 396 412 424 444 448
488 515 555 636 648 666 728 777 784 824
848 864 888 936 999</pre>
 
=={{header|J}}==
9,655

edits