Skip to content

Instantly share code, notes, and snippets.

@zfogg
Created April 5, 2012 03:24
Show Gist options
  • Save zfogg/2307719 to your computer and use it in GitHub Desktop.
Save zfogg/2307719 to your computer and use it in GitHub Desktop.
Finding Pythagorean Triplets
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