# Haskell Quiz/Weird Numbers/Solution Ninju

### From HaskellWiki

< Haskell Quiz | Weird Numbers(Difference between revisions)

m |
|||

(2 intermediate revisions by one user not shown) | |||

Line 1: | Line 1: | ||

+ | [[Category:Haskell Quiz solutions|Weird Numbers]] |
||

+ | |||

<haskell> |
<haskell> |
||

module Main where |
module Main where |
||

Line 8: | Line 10: | ||

if length args == 1 |
if length args == 1 |
||

then do putStrLn $ "The weird numbers up to " ++ (head args) ++ ":" |
then do putStrLn $ "The weird numbers up to " ++ (head args) ++ ":" |
||

− | putStrLn $ show $ takeWhile (<= max) weirdNumbers |
+ | print $ takeWhile (<= max) weirdNumbers |

else putStrLn "Usage: weird_numbers LIMIT" |
else putStrLn "Usage: weird_numbers LIMIT" |
||

− | return () |
||

weirdNumbers :: [Integer] |
weirdNumbers :: [Integer] |

## Latest revision as of 07:51, 13 December 2009

module Main where import System.Environment main :: IO () main = do args <- getArgs let max = read (head args) if length args == 1 then do putStrLn $ "The weird numbers up to " ++ (head args) ++ ":" print $ takeWhile (<= max) weirdNumbers else putStrLn "Usage: weird_numbers LIMIT" weirdNumbers :: [Integer] weirdNumbers = filter weird [70..] where weird n = sum (divisors n) > n && (not . any (==n) . map sum . subsets . divisors) n divisors :: Integer -> [Integer] divisors n = filter ((==0) . (mod n)) [1..(n `div` 2)] subsets :: [a] -> [[a]] subsets [] = [[]] subsets (x:xs) = subsets xs ++ map (x:) (subsets xs)