Created
April 5, 2012 03:24
-
-
Save zfogg/2307719 to your computer and use it in GitHub Desktop.
Finding Pythagorean Triplets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
import System.Environment | |
isPythTriplet :: (Integer, Integer, Integer) -> Bool | |
isPythTriplet (a, b, c) = (a^2) + (b^2) == (c^2) | |
-- All possible representations of three elements from xs, | |
-- where let xs[x] = indexof x in xs[a] < xs[b] < xs[c]. | |
triplets :: [Integer] -> [(Integer, Integer, Integer)] | |
triplets xs = [ (a, b, c) | a:as <- tails xs, | |
b:bs <- tails as, | |
c <- bs ] | |
-- Run like "$ ./pyth_triplets 1000" to find the triplets <= 1000 | |
main = do | |
args <- getArgs | |
let limit = read (args !! 0) :: Integer | |
print $ filter isPythTriplet (triplets [1..limit]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment