我在項目歐拉做question 62並想出了下面來測試一個數是否爲立方:可靠的立方根
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
任何的優化,也歡迎;-)
感謝您的幫助 –