Cuban primes: Difference between revisions

m
→‎{{header|Haskell}}: Pruned an import, added a list comprehension variant.
m (→‎{{header|Haskell}}: Pruned an import, added a list comprehension variant.)
Line 1,007:
Uses Data.Numbers.Primes library: http://hackage.haskell.org/package/primes-0.2.1.0/docs/Data-Numbers-Primes.html<br>
Uses Data.List.Split library: https://hackage.haskell.org/package/split-0.2.3.4/docs/Data-List-Split.html
<lang haskell>import ControlData.MonadNumbers.Primes (guardisPrime)
import Data.Numbers.Primes (isPrime)
import Data.List (intercalate)
import Data.List.Split (chunksOf)
Line 1,014 ⟶ 1,013:
 
cubans :: [Int]
cubans = filter isPrime . map (\x -> (succ x ^ 3) - (x ^ 3)) $ [1 ..]
 
main :: IO ()
main = do
mapM_ (\row -> mapM_ (printf "%10s" . thousands) row >> printf "\n") $ rows cubans
printf "\nThe 100,000th cuban prime is: %10s\n" $ thousands $ cubans !! 99999
where
where
rows = chunksOf 10 . take 200
thousands = reverse . intercalate "," . chunksOf 3 . reverse . show</lang>
 
Where filter over map could also be expressed in terms of concatMap, or a list comprehension:
 
<lang haskell>cubans :: [Int]
cubans =
[ x
| n <- [1 ..]
, let x = (succ n ^ 3) - (n ^ 3)
, isPrime x ]</lang>
 
{{out}}
<pre>
9,659

edits