Created
October 29, 2017 07:12
-
-
Save craftybones/f2fa766ce21fdf83932754857a665eac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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