Permalink
Browse files

refactored 21 for performance

  • Loading branch information...
1 parent b351c30 commit f8b348825c376625e68a3627617108a40c5137e4 Eric Wilson committed Feb 12, 2011
Showing with 4 additions and 7 deletions.
  1. +4 −7 021.hs
View
@@ -1,12 +1,9 @@
-import EulerMath.Divisors
-
--- I could optimize this, or I could move on to other problems . . .
-properDivs :: (Integral a) => a -> [a]
-properDivs n = [ d | d <- [1..n2], mod n d == 0 ]
- where n2 = quot n 2
+properDivPairs :: (Integral a) => a -> [(a,a)]
+properDivPairs n = takeWhile (\(a, b) -> a <= b) [ (d, quot n d) | d <- [1..quot n 2], mod n d == 0 ]
d :: (Integral a) => a -> a
-d = sum . properDivs
+d n = divisorSum n - n
+ where divisorSum = sum . map (uncurry (+)) . properDivPairs
hasAmicablePair :: (Integral a) => a -> Bool
hasAmicablePair n = d dn == n && n /= dn

0 comments on commit f8b3488

Please sign in to comment.