Numbers with prime digits whose sum is 13: Difference between revisions
Numbers with prime digits whose sum is 13 (view source)
Revision as of 04:31, 22 October 2020
, 3 years ago→{{header|Haskell}}: Parameterized target sum, formatted output.
(→{{header|Phix}}: iterative version) |
(→{{header|Haskell}}: Parameterized target sum, formatted output.) |
||
Line 196:
As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page.
<lang haskell>import Data.List.Split (chunksOf)
import Data.List (intercalate, transpose, unfoldr)
import Text.Printf
where
primeDigits = [2, 3, 5, 7]
go :: [[Int]] -> Maybe ([Int], [[Int]])
go xs
| null xs = Nothing
| otherwise = Just (
nextLength :: [[Int]] -> ([Int], [[Int]])
let harvest nv =
[ unDigits $ fst nv
|
prune nv =
[ fst nv
|
in ((,) . concatMap harvest <*> concatMap prune)
(((,) <*> sum) <$> ((<$> xs) . (<>) . return =<< primeDigits))
--------------------------- TEST -------------------------
unDigits :: [Int] -> Int▼
unDigits = foldl ((+) . (10 *)) 0▼
main :: IO ()
main = do
let n = 13
xs = primeDigitsNumsSummingToN n
mapM_
putStrLn
[ concat
[ (show . length) xs
, " numbers with prime digits summing to "
, show n
, ":\n"
]
, table " " $ chunksOf 10 (show <$> xs)
]
table :: String -> [[String]] -> String
table gap rows =
let ic = intercalate
ws = maximum . fmap length <$> transpose rows
pw = printf . flip ic ["%", "s"] . show
in unlines $ ic gap . zipWith pw ws <$> rows
▲unDigits :: [Int] -> Int
▲unDigits = foldl ((+) . (10 *)) 0</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
=={{header|JavaScript}}==
|