私はプロジェクトオイラーで質問62を行っており、数値が3次かどうかをテストするために次のことを思い付きました:
isInt x = x == fromInteger (round x)
isCube x= isInt $ x**(1/3)
しかし、浮動小数点エラーが原因で、正しくない結果が返されます。
*Main> isCube (384^3)
False
より信頼性の高いキューブ テストを実装する方法はありますか?
補足として、これが私のソリューションの残りの部分です。これは、 の型インターフェイス エラーのために機能しませんfilter (isCube) (perms n)
。
cubes = [n^3|n<-[1..]]
perms n = map read $ permutations $ show n :: [Integer]
answer = head [n|n<-cubes,(length $ filter (isCube) (perms n)) == 5]
エラーを修正するにはどうすればよいですか?
No instances for (Floating Integer, RealFrac Integer)
arising from a use of `isCube' at prob62.hs:10:44-49
最適化も大歓迎です;-)