Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

#7 solved, needs refactoring

  • Loading branch information...
commit dcec650ccd0d2b66d1265495021c0c6fd804a73b 1 parent bb2c623
Eric Wilson authored
Showing with 27 additions and 0 deletions.
  1. +27 −0 007.hs
View
27 007.hs
@@ -0,0 +1,27 @@
+simpleIsPrime :: Int -> Bool
+simpleIsPrime n = null $ smallFactors n
+ where smallFactors n = takeWhile (\x -> x^2 <= n) [d | d <- [2..n-1], mod n d == 0 ]
+
+findPrime :: Int -> Int
+findPrime 1 = 2
+findPrime n = filter simpleIsPrime [3,5..] !! (n - 2)
+
+relPrime :: Int -> Int -> Bool
+relPrime d n = mod n d /= 0
+
+sieve :: [Int] -> [Int] -> [Int]
+sieve [] ys = ys
+sieve (x:xs) ys = x : sieve xs (filter (relPrime x) ys)
+
+sieveRange :: [Int] -> Int -> Int -> [Int]
+sieveRange xs n m = sieve xs [n..m]
+
+primes4 :: [Int]
+primes4 = iterSieve [2,3] 4
+
+iterSieve :: [Int] -> Int -> [Int]
+iterSieve xs 0 = xs
+iterSieve xs n = iterSieve (sieveRange xs (last xs) ((last xs)^2)) (n-1)
+
+euler7 :: Int -> Int
+euler7 n = primes4 !! (n - 1)
Please sign in to comment.
Something went wrong with that request. Please try again.