Primes which contain only one odd digit: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Added a Haskell version)
Line 237: Line 237:
There are 2,560 primes under 1,000,000 which contain only one odd digit.
There are 2,560 primes under 1,000,000 which contain only one odd digit.
</pre>
</pre>

=={{header|Haskell}}==
<lang haskell>import Data.List (intercalate, maximum, transpose)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (primes)
import Text.Printf (printf)


------------------ ONE ODD DECIMAL DIGIT -----------------

singleOddDecimalDigit :: Int -> Bool
singleOddDecimalDigit =
(1 ==) . length . filter odd . digits

digits :: Int -> [Int]
digits = fmap (read . return) . show


--------------------------- TEST -------------------------
main :: IO ()
main = do
putStrLn "Below 1000:"
(putStrLn . table " " . chunksOf 10 . fmap show) $
sampleBelow 1000

putStrLn "Count of matches below 10E6:"
(print . length) $
sampleBelow 1000000

sampleBelow :: Int -> [Int]
sampleBelow =
filter singleOddDecimalDigit
. flip takeWhile primes
. (>)

------------------------- DISPLAY ------------------------

table :: String -> [[String]] -> String
table gap rows =
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</lang>
{{Out}}
<pre>Below 1000:
3 5 7 23 29 41 43 47 61 67
83 89 223 227 229 241 263 269 281 283
401 409 421 443 449 461 463 467 487 601
607 641 643 647 661 683 809 821 823 827
829 863 881 883 887

Count of matches below 10E6:
2560</pre>


=={{header|Julia}}==
=={{header|Julia}}==