Pythagorean Tuples, by Paul Franklin || #4 || I'm generating all tuples (a,b,c) that match a^2+b^2 = c^2, || sorted by c, where a,b,c>=0, b>=a || Several optimizations are possible... || We know b != a, and that a,b>1 (maybe even a,b>2). || We could keep track of a^2, b^2, c^2, to avoid constantly || evaluating that. pyth_merge ((xa,xb):xt) ((ya,yb):yt) = (xa,xb):(pyth_merge xt ((ya,yb):yt)), (xa*xa+xb*xb)<(ya*ya+yb*yb) = (ya,yb):(pyth_merge yt ((xa,xb):xt)), otherwise || pyth_tuples must spit out the first one || so pyth_merge will start spitting out stuff pyth_tuples a = (a,a):(pyth_merge (pyth_tuples (a+1)) [(a,n) | n <- [(a+1)..]]) pyth_filt ((a,b):abt) c = (a,b,c):(pyth_filt abt c), ((a*a)+(b*b))=(c*c) = pyth_filt ((a,b):abt) (c+1), ((a*a)+(b*b))>(c*c) = pyth_filt abt c, otherwise pyth = pyth_filt (pyth_tuples 1) 1