Cousin primes: Difference between revisions

→‎{{header|Haskell}}: Added a Haskell version
(Added AppleScript.)
(→‎{{header|Haskell}}: Added a Haskell version)
Line 854:
41 pairs found
</pre>
 
=={{header|Haskell}}==
<lang haskell>import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (isPrime, primes)
import Text.Printf (printf)
 
 
cousinPrimes :: [(Integer, Integer)]
cousinPrimes = concat $ (zipWith go <*> fmap (+ 4)) primes
where
go a b
| isPrime b = [(a, b)]
| otherwise = []
 
 
--------------------------- TEST -------------------------
main :: IO ()
main = do
let cousins = takeWhile ((< 1000) . snd) cousinPrimes
mapM_
putStrLn
[ (show . length) cousins <> " cousin prime pairs:",
"",
table " " $
chunksOf 5 $ show <$> cousins
]
 
------------------------ FORMATTING ----------------------
 
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>41 cousin prime pairs:
 
(3,7) (7,11) (13,17) (19,23) (37,41)
(43,47) (67,71) (79,83) (97,101) (103,107)
(109,113) (127,131) (163,167) (193,197) (223,227)
(229,233) (277,281) (307,311) (313,317) (349,353)
(379,383) (397,401) (439,443) (457,461) (463,467)
(487,491) (499,503) (613,617) (643,647) (673,677)
(739,743) (757,761) (769,773) (823,827) (853,857)
(859,863) (877,881) (883,887) (907,911) (937,941)
(967,971)</pre>
 
=={{header|J}}==
9,655

edits