Numbers with prime digits whose sum is 13: Difference between revisions

→‎{{header|Haskell}}: As an unfold, in the pattern of Nigel Galloway's recursion.
m (→‎{{header|REXX}}: simplified code.)
(→‎{{header|Haskell}}: As an unfold, in the pattern of Nigel Galloway's recursion.)
Line 178:
[337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222]
</pre>
 
=={{header|Haskell}}==
As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page.
<lang haskell>import Data.List (sort, unfoldr)
import Data.List.Split (chunksOf)
 
primeDigitsNumsSummingTo13 :: [Int]
primeDigitsNumsSummingTo13 = sort $ concat $ unfoldr go (return <$> primeDigits)
where
primeDigits = [2, 3, 5, 7]
step xs =
let nvs =
fmap ((,) <*> sum) ((<$> xs) . flip (<>) . return =<< primeDigits)
in ( nvs >>=
(\nv ->
[ unDigits $ fst nv
| 13 == snd nv ])
, nvs >>=
(\nv ->
[ fst nv
| 12 > snd nv ]))
go xs
| null xs = Nothing
| otherwise = Just (step xs)
 
unDigits :: [Int] -> Int
unDigits = foldl ((+) . (10 *)) 0
 
main :: IO ()
main =
mapM_ (putStrLn . unwords) $ chunksOf 6 (show <$> primeDigitsNumsSummingTo13)</lang>
{{Out}}
<pre>337 355 373 535 553 733
2227 2272 2335 2353 2533 2722
3235 3253 3325 3352 3523 3532
5233 5323 5332 7222 22225 22252
22333 22522 23233 23323 23332 25222
32233 32323 32332 33223 33232 33322
52222 222223 222232 222322 223222 232222
322222</pre>
 
=={{header|Julia}}==
9,659

edits