Skip to content

Instantly share code, notes, and snippets.

@craftybones
Created October 29, 2017 07:12
Show Gist options
  • Save craftybones/f2fa766ce21fdf83932754857a665eac to your computer and use it in GitHub Desktop.
Save craftybones/f2fa766ce21fdf83932754857a665eac to your computer and use it in GitHub Desktop.
by6 :: Int -> [Int]
by6 n = [(6*n)-1,(6*n)+1]
genPrimeCandidates :: Int -> [Int]
genPrimeCandidates n = (by6 n) ++ genPrimeCandidates (n+1)
primeCandidates :: [Int]
primeCandidates = [2,3] ++ (genPrimeCandidates 1)
primeCandidatesBelow :: Int -> [Int]
primeCandidatesBelow n = takeWhile (<n) primeCandidates
divisibleBy :: Int -> Int -> Bool
divisibleBy x y = (rem x y) == 0
closestIntegerSqrt = floor . sqrt .fromIntegral
isPrime :: Int -> Bool
isPrime x =
let t = closestIntegerSqrt x
xNotDivisibleBy = not . (divisibleBy x)
in all xNotDivisibleBy (primeCandidatesBelow t)
primes :: [Int]
primes = filter isPrime primeCandidates
fibo :: [Int]
fibo =
let fibo' :: Int -> Int -> [Int]
fibo' a b = a:(fibo' b (a+b))
in (fibo' 0 1)
nthFibo = last . (flip take fibo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment