output = lay (map show pyths) pyths = [(a, b, intsqrt (a*a+b*b)) // a <- [3..]; b<-[a+1..]; is_sq (a*a+b*b)] intsqrt x = entier (sqrt x) is_sq y = (intsqrt y) ^ 2 = y
Finds all pythagorean triangles (right triangles with integer sides)
Note the use of a diagonalising list comprehension, with //
instead of |
as separator. To see the triangles, say
outputThe output looks like this:
(3,4,5) (6,8,10) (5,12,13) (9,12,15) (8,15,17) (12,16,20) (15,20,25) (20,21,29) (7,24,25) (10,24,26) (18,24,30) (21,28,35) (16,30,34) (24,32,40) (12,35,37) (15,36,39) (27,36,45) (9,40,41) (30,40,50) (40,42,58) ..... etc