Skip to content

Instantly share code, notes, and snippets.

@mattneary
Last active December 11, 2015 06:39
Show Gist options
  • Save mattneary/4561089 to your computer and use it in GitHub Desktop.
Save mattneary/4561089 to your computer and use it in GitHub Desktop.
Haskell Array-based isPrime
module Main where
grid range = [[i,j] | i <- range, j <- range] -- construct grid of number pairs
multiplicationTable grid = map (\l -> foldl (*) 1 l) grid -- multiply said pairs
isPrime number = not $ any (==number) $ (multiplicationTable . grid) [2 .. number] -- check for presence in table
main = print $ map (\p -> (fst p, (isPrime . fst) p)) [(i, b) | i <- [1 .. 100], b <- [True]] -- map all numbers to primeness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment