13 Jan
2008
13 Jan
'08
6:15 a.m.
On Sat, 12 Jan 2008, Achim Schneider wrote:
"Rafael Almeida" <almeidaraf@gmail.com> wrote:
perfectSquares :: [Integer] perfectSquares = zipWith (*) [1..] [1..]
isPerfectSquare :: Integer -> Bool isPerfectSquare x = (head $ dropWhile (<x) perfectSquares) == x
what about
module Main where
isPerfectSquare :: Integer -> Bool isPerfectSquare n = sqrrt == fromIntegral (truncate sqrrt) where sqrrt = sqrt $ fromIntegral n
? It's a hell alot faster, but I have no idea if some numerical property of square roots could make it give different results than your version, in rare cases.
The rare cases occur for big numbers. http://www.haskell.org/haskellwiki/Generic_number_type#isSquare