taxicab.hs (916B)
1 -- Spec: write a function that returns all of the ways a number can be written 2 -- as the sum of two non-negative cubes. 3 4 {-# OPTIONS_GHC -Wall -Werror #-} 5 6 import Control.Arrow 7 import Test.QuickCheck 8 9 cubeDecomposition :: Int -> [(Int, Int)] 10 cubeDecomposition n = 11 [(x, y) | x <- [1..m], y <- [x..m], x^(3 :: Int) + y^(3 :: Int) == n] 12 where m = truncate $ fromIntegral n ** (1/3 :: Double) 13 14 main :: IO () 15 main = do 16 let highConfidence = stdArgs {maxSuccess = 10000} 17 putStrLn "running tests.." 18 quickCheckWith highConfidence (forAll smallInts cubedElementsSumToN) 19 20 -- Tests ----------------------------------------------------------------------- 21 22 smallInts :: Gen Int 23 smallInts = choose (-100000000, 100000000) 24 25 cubedElementsSumToN :: Int -> Bool 26 cubedElementsSumToN n = all (== n) d 27 where d = map (uncurry (+) . ((^(3 :: Int)) *** (^(3 :: Int)))) 28 (cubeDecomposition n) 29