Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Problem twelve, and a utility module

  • Loading branch information...
commit 064e4e77def70dbb2ac371a70d64c50b80a2db3c 1 parent e5104f3
Eric Wilson authored
Showing with 17 additions and 24 deletions.
  1. +7 −24 012.hs
  2. +10 −0 SimpleMath.hs
View
31 012.hs
@@ -1,31 +1,14 @@
-divisors :: Integer -> [Integer]
-divisors n = [ d | d <- [1..n], mod n d == 0 ]
-
-numDivs :: Integer -> Int
-numDivs = length . divisors
+import SimpleMath
tri :: Integer -> Integer
tri n = n * (n+1) `quot` 2
-hm :: Integer -> Int
-hm n = if (even n)
+triDivs :: Integer -> Int
+triDivs n = if (even n)
then numDivs (quot n 2) * numDivs (n+1)
else numDivs n * numDivs (quot (n+1) 2)
-contenders :: Integer -> [Integer]
-contenders n = twosMap [1..n]
-
-twosMap :: [Integer] -> [Integer]
-twosMap [] = []
-twosMap (x:xs) = twos x ++ twosMap xs
-
-twos :: Integer -> [Integer]
-twos n = (2^n - 1) : [2^n]
-
-triDivTriple :: Integer -> (Integer, Integer, Int)
-triDivTriple n = (n, tri n, hm n)
-
-tripleAbove :: Int -> (Integer, Integer, Int) -> Bool
-tripleAbove n (_, _, m) = (m > n)
-
-{-- filter (tripleAbove 500) $ map triDivTriple $ contenders 20 --}
+euler12 :: Int -> (Integer, Integer, Int)
+euler12 n = head $ filter (tripleAbove n) $ map triDivTriple [1..]
+ where triDivTriple m = (m, tri m, triDivs m)
+ tripleAbove n (_, _, m) = (m > n)
View
10 SimpleMath.hs
@@ -0,0 +1,10 @@
+module SimpleMath
+( divisors
+, numDivs
+) where
+
+divisors :: Integer -> [Integer]
+divisors n = [ d | d <- [1..n], mod n d == 0 ]
+
+numDivs :: Integer -> Int
+numDivs = length . divisors
Please sign in to comment.
Something went wrong with that request. Please try again.