Ejercicio de flatmap

Deseo generar las tripletas x,y,z tales que \(z^2 = x^2 + y^2\) que esten en el rango 1 y 1000, tambien quiero esta \(z^3 = x^3 + y^3\), tambien \(\sqrt{z} = \sqrt{x} + \sqrt{y}\)

De los cuadrados

scala> (1 to m) flatMap (x => (1 to m) flatMap (y => (1 to m) map (z => (x,y,z)))) filter (t => t._1*t._1 + t._2*t._2 == t._3*t._3)
val res18: IndexedSeq[(Int, Int, Int)] = Vector((3,4,5), (4,3,5), (5,12,13), (6,8,10), (7,24,25), (8,6,10), (8,15,17), (9,12,15), (9,40,41), (10,24,26), (11,60,61), (12,5,13), (12,9,15), (12,16,20), (12,35,37), (13,84,85), (14,48,50), (15,8,17), (15,20,25), (15,36,39), (16,12,20), (16,30,34), (16,63,65), (18,24,30), (18,80,82), (20,15,25), (20,21,29), (20,48,52), (21,20,29), (21,28,35), (21,72,75), (24,7,25), (24,10,26), (24,18,30), (24,32,40), (24,45,51), (24,70,74), (25,60,65), (27,36,45), (28,21,35), (28,45,53), (28,96,100), (30,16,34), (30,40,50), (30,72,78), (32,24,40), (32,60,68), (33,44,55), (33,56,65), (35,12,37), (35,84,91), (36,15,39), (36,27,45), (36,48,60), (36,77,85), (39,52,65), (39,80,89), (40,9,41), (40,30,50), (40,42,58), (40,75,85), (42,40,58)...

De los cubos

scala> (1 to m) flatMap (x => (1 to m) flatMap (y => (1 to m) map (z => (x,y,z)))) filter (t => t._1*t._1*t._1 + t._2*t._2*t._2 == t._3*t._3*t._3)
val res19: IndexedSeq[(Int, Int, Int)] = Vector()

El de la raiz

scala> (1 to m) flatMap (x => (1 to m) flatMap (y => (1 to m) map (z => (x,y,z)))) filter (t => Math.sqrt(t._1) + Math.sqrt(t._2) == Math.sqrt(t._3))
val res20: IndexedSeq[(Int, Int, Int)] = Vector((1,1,4), (1,4,9), (1,9,16), (1,16,25), (1,25,36), (1,36,49), (1,49,64), (1,64,81), (1,81,100), (2,2,8), (2,32,50), (2,72,98), (3,3,12), (3,12,27), (3,27,48), (4,1,9), (4,4,16), (4,9,25), (4,16,36), (4,25,49), (4,36,64), (4,49,81), (4,64,100), (5,5,20), (5,20,45), (5,45,80), (6,6,24), (6,54,96), (7,7,28), (7,28,63), (8,8,32), (8,18,50), (8,50,98), (9,1,16), (9,4,25), (9,9,36), (9,16,49), (9,25,64), (9,36,81), (9,49,100), (10,10,40), (10,40,90), (11,11,44), (11,44,99), (12,3,27), (12,12,48), (13,13,52), (14,14,56), (15,15,60), (16,1,25), (16,4,36), (16,9,49), (16,16,64), (16,25,81), (16,36,100), (17,17,68), (18,8,50), (18,18,72), (18,32,98), (19,19,76), (20,5,45), (20,20,80), (21,21,84), (22,22,88), (23,23,92), (24,...