Last active
July 13, 2016 17:10
-
-
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`
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
-- 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