
10 Mar
2010
10 Mar
'10
9:18 a.m.
Am Mittwoch 10 März 2010 14:53:32 schrieb Stephen Tetley:
Hello all
Algorithmically oddSquareSum is a bit below par though...
oddSquareSum :: Integer oddSquareSum = sum . takeWhile (<10000) . filter odd . map (^2) $ [1..]
Why filter out the evens after generating them?
oos1 :: Integer oss1 = sum . takeWhile (<10000) $ map (^2) odds where odds = iterate (+2) 1
Since we're now taking the code apart, oddSquareSum2 :: Integer oddSquareSum2 = sumOddSquaresBelow 10000 sumOddSquaresBelow bound = takeWhile (< bound) [x*x | x <- [1, 3 .. ]] (but you'd really want to use the power-sum formula).
Best wishes
Stephen