Haskell Quiz/Weird Numbers/Solution Bailey
(Added my power set solution)
|(One intermediate revision by one user not shown)|
Latest revision as of 07:25, 11 February 2010
The implementation below isn't very efficient, apparently (the thread referred to in the comments talks about why not), or particularly fast, but it does work.
The heart of the solution is the is_weird function. It takes a number and determines if its weird. The first weird number is 70, so all numbers below that are thrown out.
If the number is greater than 70, I first find all the divisors up to n / 2 for that number. Using powerSet, I then find all possible subsets of those divisors.
Finally, the function first ensures the sum of all the divisors is greater than n. Next, it searches all the subsets and determines if any of them sum to the value of n. If not, the number is weird!
Optimizations might include generating the powerset such that the longest lists come first, to find numbers that aren't weird sooner.
A bonus function, find_weird, takes an argument and finds the first n weird numbers.
-- powerSet found courtesy of http://haskell.org/hawiki/PreludeExts -- originally from http://www.haskell.org/pipermail/haskell-cafe/2003-June/004484.html powerSet :: [a] -> [[a]] powerSet  = [] powerSet (x:xs) = xss ++ map (x:) xss where xss = powerSet xs -- Only positive numbers. 70 is first weird number, so exclude all below 69. is_weird n | n < 70 = False is_weird n = let divisors = [d | d <- [1..(n `div` 2)], n `mod` d == 0] divisor_subsets = powerSet divisors in sum divisors > n && not (any (\subset -> sum subset == n) divisor_subsets) find_weird n = take n [w | w <- [70..], is_weird w]