[Haskell-cafe] Re: Solving a geometry problem with Haskell
Henning Thielemann
lemming at henning-thielemann.de
Sat Jan 12 17:15:18 EST 2008
On Sat, 12 Jan 2008, Achim Schneider wrote:
> "Rafael Almeida" <almeidaraf at 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
More information about the Haskell-Cafe
mailing list