Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactored libraries, added #10

  • Loading branch information...
commit 2ec7abb13664acec5bc5876a5e255de9823e5449 1 parent dcec650
authored January 11, 2011
2  002.hs
... ...
@@ -1,4 +1,4 @@
1  
-import SimpleMath
  1
+import EulerMath.Fib
2 2
 
3 3
 euler2 :: Int -> Int
4 4
 euler2 n = sum $ filter even $ takeWhile (< n) [ fib k | k <- [1..] ]
26  007.hs
... ...
@@ -1,27 +1,5 @@
1  
-simpleIsPrime :: Int -> Bool
2  
-simpleIsPrime n = null $ smallFactors n
3  
-	where smallFactors n = takeWhile (\x -> x^2 <= n) [d | d <- [2..n-1], mod n d == 0 ]
4  
-	
5  
-findPrime :: Int -> Int
6  
-findPrime 1 = 2
7  
-findPrime n = filter simpleIsPrime [3,5..] !! (n - 2)
  1
+import EulerMath.Primes
8 2
 
9  
-relPrime :: Int -> Int -> Bool
10  
-relPrime d n = mod n d /= 0
11  
-
12  
-sieve :: [Int] -> [Int] -> [Int]
13  
-sieve [] ys = ys
14  
-sieve (x:xs) ys = x : sieve xs (filter (relPrime x) ys)
15  
-
16  
-sieveRange :: [Int] -> Int -> Int -> [Int]
17  
-sieveRange xs n m = sieve xs [n..m]
18  
-
19  
-primes4 :: [Int]
20  
-primes4 = iterSieve [2,3] 4
21  
-
22  
-iterSieve :: [Int] -> Int -> [Int]
23  
-iterSieve xs 0 = xs
24  
-iterSieve xs n = iterSieve (sieveRange xs (last xs) ((last xs)^2)) (n-1)
25 3
 
26 4
 euler7 :: Int -> Int
27  
-euler7 n = primes4 !! (n - 1)
  5
+euler7 n = last $ nPrimes n
4  010.hs
... ...
@@ -0,0 +1,4 @@
  1
+import EulerMath.Primes
  2
+
  3
+euler10 :: Int -> Int
  4
+euler10 = sum . primesUpTo
10  012.hs
... ...
@@ -1,4 +1,5 @@
1  
-import SimpleMath
  1
+import EulerMath.Divisors
  2
+import EulerMath.Primes
2 3
 
3 4
 tri :: Integer -> Integer
4 5
 tri n = n * (n+1) `quot` 2
@@ -10,5 +11,8 @@ triDivs n = if (even n)
10 11
 
11 12
 euler12 :: Int -> (Integer, Integer, Int)
12 13
 euler12 n = head $ filter (tripleAbove n) $ map triDivTriple [1..]
13  
-	where triDivTriple m = (m, tri m, triDivs m)
14  
-		  tripleAbove n (_, _, m) = (m > n)
  14
+	where 
  15
+		triDivTriple m = (m, tri m, triDivs m)
  16
+		tripleAbove n (_, _, m) = (m > n)
  17
+
  18
+	
27  EulerMath/Divisors.hs
... ...
@@ -0,0 +1,27 @@
  1
+module EulerMath.Divisors  
  2
+( divisors  
  3
+, numDivs
  4
+, primeFactorization
  5
+) where 
  6
+
  7
+import EulerMath.Primes
  8
+
  9
+divisors :: Integer -> [Integer]
  10
+divisors n = [ d | d <- [1..n], mod n d == 0 ]
  11
+
  12
+numDivs :: Integer -> Int
  13
+numDivs = length . divisors
  14
+
  15
+primeFactors :: Int -> [Int]
  16
+primeFactors n = [ d | d <- primesUpTo n, mod n d == 0 ]
  17
+
  18
+timesDivides :: Int -> Int -> Int
  19
+timesDivides d n = timesDividesIt d n 0
  20
+	where 
  21
+		timesDividesIt d n i = 
  22
+			if (mod n d /= 0)
  23
+				then i
  24
+				else timesDividesIt d (quot n d) (i+1)
  25
+
  26
+primeFactorization :: Int -> [(Int, Int)]
  27
+primeFactorization n = map (\p -> (p, timesDivides p n)) $ primeFactors n
10  EulerMath/Fib.hs
... ...
@@ -0,0 +1,10 @@
  1
+module EulerMath.Fib  
  2
+( fib
  3
+) where 
  4
+
  5
+fib :: Int -> Int
  6
+fib n = fst $ fibPair n
  7
+	where 
  8
+		fibPair 1 = (1,1)
  9
+		fibPair n = fibNext $ fibPair (n-1)
  10
+		fibNext (x,y) = (y, x+y)
32  EulerMath/Primes.hs
... ...
@@ -0,0 +1,32 @@
  1
+module EulerMath.Primes  
  2
+( nPrimes
  3
+, primesUpTo  
  4
+, primesBelowRoot
  5
+, isPrime
  6
+) where 
  7
+
  8
+divides :: Int -> Int -> Bool
  9
+divides d n = mod n d == 0
  10
+
  11
+sieve :: [Int] -> [Int] -> [Int]
  12
+sieve [] ys = ys
  13
+sieve (x:xs) ys = x : sieve xs (filter (not . divides x) ys)
  14
+
  15
+genPrimes :: Int -> [Int]
  16
+genPrimes n = iterSieve [2,3] n
  17
+	where
  18
+		iterSieve xs 0 = xs
  19
+		iterSieve xs n = iterSieve (sieveRange xs (last xs) ((last xs)^2)) (n-1)
  20
+		sieveRange xs n m = sieve xs [n..m]
  21
+
  22
+nPrimes :: Int -> [Int]
  23
+nPrimes n = take n $ genPrimes 4
  24
+
  25
+primesUpTo :: Int -> [Int]
  26
+primesUpTo n = takeWhile (<=n) $ genPrimes 4
  27
+
  28
+primesBelowRoot :: Int -> [Int]
  29
+primesBelowRoot n = takeWhile (\d -> d^2 <= n) $ genPrimes 4
  30
+
  31
+isPrime :: Int -> Bool
  32
+isPrime n = any (==n) $ primesUpTo (n+1)
3  SimpleMath.hs
... ...
@@ -1,6 +1,5 @@
1 1
 module SimpleMath  
2  
-( fib
3  
-, divisors  
  2
+( divisors  
4 3
 , numDivs
5 4
 ) where 
6 5
 

0 notes on commit 2ec7abb

Please sign in to comment.
Something went wrong with that request. Please try again.