Skip to content

Instantly share code, notes, and snippets.

@mbjd
Last active July 13, 2016 17:10
Show Gist options
  • Save mbjd/c345544fd88750ac524d3f37e792dd84 to your computer and use it in GitHub Desktop.
Save mbjd/c345544fd88750ac524d3f37e792dd84 to your computer and use it in GitHub Desktop.
Find numbers that are both square and triangle numbers. To use, start ghci and execute `:l tri_squares.hs`, followed by `tri_squares`
-- find numbers that are both a square and a triangle number.
-- https://www.youtube.com/watch?v=Gh8h8MJFFdI
squares = map (\x -> x * x) [0..]
triangle_number x = div (x * (x + 1)) 2
triangle_numbers = map triangle_number [0..]
-- Check if you can arrange x points in a triangle with an integer side length
is_triangle x =
x == last (takeWhile (<= x) triangle_numbers)
tri_squares = [i | i <- squares, is_triangle i]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment