Skip to content

Instantly share code, notes, and snippets.

@raffitz
Created July 13, 2016 15:10
Show Gist options
  • Save raffitz/d0a6f60ec2025e0f6585cc51711051d0 to your computer and use it in GitHub Desktop.
Save raffitz/d0a6f60ec2025e0f6585cc51711051d0 to your computer and use it in GitHub Desktop.
Script to find triangular numbers that are simultaneously square numbers.
squares :: (Integral a) => [a]
squares = map (\x -> x*x) [1..]
triangles :: (Integral a) => [a]
triangles = map (\x -> (div (x*(x+1)) 2)) [1..]
match :: (Integral a) => [a] -> [a] -> [a]
match (a:as) (b:bs)
| a == b = a:(match as bs)
| a < b = match (dropWhile ((>) b ) as) (b:bs)
| otherwise = match (a:as) (dropWhile ((>) a) bs)
main :: IO ()
main = mapM_ (putStrLn.show) (match squares triangles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment