Saltar a contenido

Usualmente vamos a necesitar hacer operaciones que están dentro del producto cartesiano de dos o más conjuntos

\[ A \times B \]

\(A =\{1,2,3\} B = \{4,5\}, A \times B = \{(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)\}\)

scala> val A = 1 to 3
val A: scala.collection.immutable.Range.Inclusive = Range 1 to 3

scala> val B = 4 to 5
val B: scala.collection.immutable.Range.Inclusive = Range 4 to 5

scala> A map (x => B map (y => (x,y)))
val res36: IndexedSeq[IndexedSeq[(Int, Int)]] = Vector(Vector((1,4), (1,5)), Vector((2,4), (2,5)), Vector((3,4), (3,5)))

scala> (A map (x => B map (y => (x,y)))).flatten
val res38: IndexedSeq[(Int, Int)] = Vector((1,4), (1,5), (2,4), (2,5), (3,4), (3,5))

scala> A flatMap (x => B map (y => (x,y)))
val res39: IndexedSeq[(Int, Int)] = Vector((1,4), (1,5), (2,4), (2,5), (3,4), (3,5))

Ejercicio

Deseo generar todos los tripletas (i,j,k) donde i es multiplo de j, j es multiplo k, k es un número primo, deseo generar los numeros entre 1 y 1000

  1. Generar los números sin ningun tipo de restricción
scala> val m = 100
val m: Int = 100

scala> (1 to m) flatMap (i => (1 to m) flatMap (j => (1 to m) map (k => (i,j,k))))
val res45: IndexedSeq[(Int, Int, Int)] = Vector((1,1,1), (1,1,2), (1,1,3), (1,1,4), (1,1,5), (1,1,6), (1,1,7), (1,1,8), (1,1,9), (1,1,10), (1,1,11), (1,1,12), (1,1,13), (1,1,14), (1,1,15), (1,1,16), (1,1,17), (1,1,18), (1,1,19), (1,1,20), (1,1,21), (1,1,22), (1,1,23), (1,1,24), (1,1,25), (1,1,26), (1,1,27), (1,1,28), (1,1,29), (1,1,30), (1,1,31), (1,1,32), (1,1,33), (1,1,34), (1,1,35), (1,1,36), (1,1,37), (1,1,38), (1,1,39), (1,1,40), (1,1,41), (1,1,42), (1,1,43), (1,1,44), (1,1,45), (1,1,46), (1,1,47), (1,1,48), (1,1,49), (1,1,50), (1,1,51), (1,1,52), (1,1,53), (1,1,54), (1,1,55), (1,1,56), (1,1,57), (1,1,58), (1,1,59), (1,1,60), (1,1,61), (1,1,62), (1,1,63), (1,1,64), (1,1,65), (1,1,66), (1,1,67), (1,1,68), (1,1,69), (1,1,70), (1,1,71), (1,1,72), (1,1,73), (1...
  1. Filtrar que i es multiplo de j, lo que valido es que j modulo i = 0

    scala> (1 to m) flatMap (i => (1 to m) flatMap (j => (1 to m) map (k => (i,j,k)))) filter (t => t._2 % t._1 == 0)
    val res1: IndexedSeq[(Int, Int, Int)] = Vector((1,1,1), (1,1,2), (1,1,3), (1,1,4), (1,1,5), (1,1,6), (1,1,7), (1,1,8), (1,1,9), (1,1,10), (1,1,11), (1,1,12), (1,1,13), (1,1,14), (1,1,15), (1,1,16), (1,1,17), (1,1,18), (1,1,19), (1,1,20), (1,1,21), (1,1,22), (1,1,23), (1,1,24), (1,1,25), (1,1,26), (1,1,27), (1,1,28), (1,1,29), (1,1,30), (1,1,31), (1,1,32), (1,1,33), (1,1,34), (1,1,35), (1,1,36), (1,1,37), (1,1,38), (1,1,39), (1,1,40), (1,1,41), (1,1,42), (1,1,43), (1,1,44), (1,1,45), (1,1,46), (1,1,47), (1,1,48), (1,1,49), (1,1,50), (1,1,51), (1,1,52), (1,1,53), (1,1,54), (1,1,55), (1,1,56), (1,1,57), (1,1,58), (1,1,59), (1,1,60), (1,1,61), (1,1,62), (1,1,63), (1,1,64), (1,1,65), (1,1,66), (1,1,67), (1,1,68), (1,1,69), (1,1,70), (1,1,71), (1,1,72), (1,1,73), (1,...
    
  2. Filtrar de que k es multiplo j, k modulo j == 0

    scala> (1 to m) flatMap (i => (1 to m) flatMap (j => (1 to m) map (k => (i,j,k)))) filter (t => t._2 % t._1 == 0 && t._3 % t._2 == 0)
    val res2: IndexedSeq[(Int, Int, Int)] = Vector((1,1,1), (1,1,2), (1,1,3), (1,1,4), (1,1,5), (1,1,6), (1,1,7), (1,1,8), (1,1,9), (1,1,10), (1,1,11), (1,1,12), (1,1,13), (1,1,14), (1,1,15), (1,1,16), (1,1,17), (1,1,18), (1,1,19), (1,1,20), (1,1,21), (1,1,22), (1,1,23), (1,1,24), (1,1,25), (1,1,26), (1,1,27), (1,1,28), (1,1,29), (1,1,30), (1,1,31), (1,1,32), (1,1,33), (1,1,34), (1,1,35), (1,1,36), (1,1,37), (1,1,38), (1,1,39), (1,1,40), (1,1,41), (1,1,42), (1,1,43), (1,1,44), (1,1,45), (1,1,46), (1,1,47), (1,1,48), (1,1,49), (1,1,50), (1,1,51), (1,1,52), (1,1,53), (1,1,54), (1,1,55), (1,1,56), (1,1,57), (1,1,58), (1,1,59), (1,1,60), (1,1,61), (1,1,62), (1,1,63), (1,1,64), (1,1,65), (1,1,66), (1,1,67), (1,1,68), (1,1,69), (1,1,70), (1,1,71), (1,1,72), (1,1,73), (1,...
    
  3. Validar que el tercero sea primo

    scala> (1 to m) flatMap (i => (1 to m) flatMap (j => (1 to m) map (k => (i,j,k)))) filter (t => t._2 % t._1 == 0 && t._3 % t._2 == 0 && ((2 to Math.ceil(Math.sqrt(t._3)).toInt) forall (x => t._3 % x != 0 || t._3 <= 2)))
    val res7: IndexedSeq[(Int, Int, Int)] = Vector((1,1,1), (1,1,2), (1,1,3), (1,1,5), (1,1,7), (1,1,11), (1,1,13), (1,1,17), (1,1,19), (1,1,23), (1,1,29), (1,1,31), (1,1,37), (1,1,41), (1,1,43), (1,1,47), (1,1,53), (1,1,59), (1,1,61), (1,1,67), (1,1,71), (1,1,73), (1,1,79), (1,1,83), (1,1,89), (1,1,97), (1,2,2), (1,3,3), (1,5,5), (1,7,7), (1,11,11), (1,13,13), (1,17,17), (1,19,19), (1,23,23), (1,29,29), (1,31,31), (1,37,37), (1,41,41), (1,43,43), (1,47,47), (1,53,53), (1,59,59), (1,61,61), (1,67,67), (1,71,71), (1,73,73), (1,79,79), (1,83,83), (1,89,89), (1,97,97), (2,2,2), (3,3,3), (5,5,5), (7,7,7), (11,11,11), (13,13,13), (17,17,17), (19,19,19), (23,23,23), (29,29,29), (31,31,31), (37,37,37), (41,41,41), (43,43,43), (47,47,47), (53,53,53), (59,59,59), (61,61,61)...