Primes whose sum of digits is 25: Difference between revisions
Content added Content deleted
m (added a category.) |
(→{{header|Haskell}}: Added a Haskell example) |
||
Line 253: | Line 253: | ||
Took 25.300758564s |
Took 25.300758564s |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
|||
<lang haskell>import Data.Bifunctor (second) |
|||
import Data.List (replicate) |
|||
import Data.List.Split (chunksOf) |
|||
import Data.Numbers.Primes (primes) |
|||
--------- PRIMES WITH DECIMAL DIGITS SUMMING TO 25 ------- |
|||
matchingPrimes :: [Int] |
|||
matchingPrimes = |
|||
takeWhile |
|||
(< 5000) |
|||
[n | n <- primes, 25 == decimalDigitSum n] |
|||
decimalDigitSum :: Int -> Int |
|||
decimalDigitSum n = |
|||
snd $ |
|||
until |
|||
((0 ==) . fst) |
|||
(\(n, x) -> second (+ x) $ quotRem n 10) |
|||
(n, 0) |
|||
--------------------------- TEST ------------------------- |
|||
main :: IO () |
|||
main = do |
|||
let w = length (show (last matchingPrimes)) |
|||
mapM_ putStrLn $ |
|||
( show (length matchingPrimes) |
|||
<> " primes (< 5000) with decimal digits totalling 25:\n" |
|||
) : |
|||
( unwords |
|||
<$> chunksOf |
|||
4 |
|||
(justifyRight w ' ' . show <$> matchingPrimes) |
|||
) |
|||
justifyRight :: Int -> Char -> String -> String |
|||
justifyRight n c = (drop . length) <*> (replicate n c <>)</lang> |
|||
{{Out}} |
|||
<pre>17 primes (< 5000) with decimal digits totalling 25: |
|||
997 1699 1789 1879 |
|||
1987 2689 2797 2887 |
|||
3499 3697 3769 3877 |
|||
3967 4597 4759 4957 |
|||
4993</pre> |
|||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |