Skip to content

Instantly share code, notes, and snippets.

@antekone
Created January 16, 2015 13:28
Show Gist options
  • Save antekone/f80dd4583c6672439124 to your computer and use it in GitHub Desktop.
Save antekone/f80dd4583c6672439124 to your computer and use it in GitHub Desktop.
prime generation by trial division testing
checkd :: [Int] -> Int -> Int
checkd [] n = n
checkd (prime:primes) n =
if (elem n primes || n `mod` prime == 0) then
prime
else
checkd primes n
preplist :: [Int] -> Int -> [Int]
preplist curlist n = filter (\ x -> x < (n `div` 2 + 1)) curlist
gen :: Int -> Int -> [Int] -> [Int]
gen a b xs =
if a <= b then gen (a + 2) b newxs else xs
where
prime = checkd (preplist xs a) a
newxs = if prime == a then
xs ++ [prime]
else
xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment