ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

Scala で Project Euler Problem 4 その2

前回のが、全然 Scala っぽくなかったので、再挑戦

scala> (1 to 9)
res0: scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
 
scala>
 
scala> (1 to 9).map(i => (i to 9))
res1: scala.collection.immutable.IndexedSeq[scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne] = Vector(Range(1, 2, 3, 4, 5, 6, 7, 8, 9), Range(2, 3, 4, 5, 6, 7, 8, 9), Range(3, 4, 5, 6, 7, 8, 9), Range(4, 5, 6, 7, 8, 9), Range(5, 6, 7, 8, 9), Range(6, 7, 8, 9), Range(7, 8, 9), Range(8, 9), Range(9))
 
scala>
 
scala> (1 to 9).map(i => (i to 9).map(j => (i, j)))
res2: scala.collection.immutable.IndexedSeq[scala.collection.immutable.IndexedSeq[(Int, Int)]] = Vector(Vector((1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9)), Vector((2,2), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (2,9)), Vector((3,3), (3,4), (3,5), (3,6), (3,7), (3,8), (3,9)), Vector((4,4), (4,5), (4,6), (4,7), (4,8), (4,9)), Vector((5,5), (5,6), (5,7), (5,8), (5,9)), Vector((6,6), (6,7), (6,8), (6,9)), Vector((7,7), (7,8), (7,9)), Vector((8,8), (8,9)), Vector((9,9)))
 
scala>
 
scala> (1 to 9).map(i => (i to 9).map(j => (i, j))).flatten
res3: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (2,2), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (2,9), (3,3), (3,4), (3,5), (3,6), (3,7), (3,8), (3,9), (4,4), (4,5), (4,6), (4,7), (4,8), (4,9), (5,5), (5,6), (5,7), (5,8), (5,9), (6,6), (6,7), (6,8), (6,9), (7,7), (7,8), (7,9), (8,8), (8,9), (9,9))
 
scala>
 
scala> (1 to 9).map(i => (i to 9).map(j => (i, j))).flatten.filter(t => t._1 % 11 == 0 || t._2 % 11 == 0)
res4: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector()
 
scala>
 
scala> (10 to 99).map(i => (i to 99).map(j => (i, j))).flatten.filter(t => t._1 % 11 == 0 || t._2 % 11 == 0)
res5: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((10,11), (10,22), (10,33), (10,44), (10,55), (10,66), (10,77), (10,88), (10,99), (11,11), (11,12), (11,13), (11,14), (11,15), (11,16), (11,17), (11,18), (11,19), (11,20), (11,21), (11,22), (11,23), (11,24), (11,25), (11,26), (11,27), (11,28), (11,29), (11,30), (11,31), (11,32), (11,33), (11,34), (11,35), (11,36), (11,37), (11,38), (11,39), (11,40), (11,41), (11,42), (11,43), (11,44), (11,45), (11,46), (11,47), (11,48), (11,49), (11,50), (11,51), (11,52), (11,53), (11,54), (11,55), (11,56), (11,57), (11,58), (11,59), (11,60), (11,61), (11,62), (11,63), (11,64), (11,65), (11,66), (11,67), (11,68), (11,69), (11,70), (11,71), (11,72), (11,73), (11,74), (11,75), (11,76), (11,77), (11,78), (11,79), (11,80), (11,81), (11,82), (11...
scala>
 
scala> (10 to 99).map(i => (i to 99).map(j => (i, j))).flatten.filter(t => t._1 % 11 == 0 || t._2 % 11 == 0).map(t => t._1 * t._2)
res6: scala.collection.immutable.IndexedSeq[Int] = Vector(110, 220, 330, 440, 550, 660, 770, 880, 990, 121, 132, 143, 154, 165, 176, 187, 198, 209, 220, 231, 242, 253, 264, 275, 286, 297, 308, 319, 330, 341, 352, 363, 374, 385, 396, 407, 418, 429, 440, 451, 462, 473, 484, 495, 506, 517, 528, 539, 550, 561, 572, 583, 594, 605, 616, 627, 638, 649, 660, 671, 682, 693, 704, 715, 726, 737, 748, 759, 770, 781, 792, 803, 814, 825, 836, 847, 858, 869, 880, 891, 902, 913, 924, 935, 946, 957, 968, 979, 990, 1001, 1012, 1023, 1034, 1045, 1056, 1067, 1078, 1089, 264, 396, 528, 660, 792, 924, 1056, 1188, 286, 429, 572, 715, 858, 1001, 1144, 1287, 308, 462, 616, 770, 924, 1078, 1232, 1386, 330, 495, 660, 825, 990, 1155, 1320, 1485, 352, 528, 704, 880, 1056, 1232, 1408, 1584, 374, 561, 748, 935, 1122,...
scala>
 
scala> (10 to 99).map(i => (i to 99).map(j => (i, j))).flatten.filter(t => t._1 % 11 == 0 || t._2 % 11 == 0).map(t => t._1 * t._2).max
res7: Int = 9801
 
scala>
 
scala> (10 to 99).map(i => (i to 99).map(j => (i, j))).flatten.filter(t => t._1 % 11 == 0 || t._2 % 11 == 0).map(t => t._1 * t._2).filter(n => n.toString == n.toString.reverse)
res8: scala.collection.immutable.IndexedSeq[Int] = Vector(121, 242, 363, 484, 616, 737, 858, 979, 1001, 858, 1001, 616, 1881, 484, 616, 858, 2002, 2112, 1771, 2112, 858, 2002, 2772, 2552, 2112, 1221, 1551, 1881, 2112, 2442, 2772, 3003, 2992, 2772, 2442, 3663, 3003, 2772, 2112, 2332, 2552, 2772, 2992, 4004, 4224, 4554, 4224, 3773, 4004, 4664, 5005, 5115, 5225, 5335, 5445, 4774, 4224, 6336, 5005, 4554, 4884, 6006, 6336, 6336, 7227, 5775, 6006, 6776, 7007, 8118, 8008, 8448, 9009)
 
scala>
 
scala> (10 to 99).map(i => (i to 99).map(j => (i, j))).flatten.filter(t => t._1 % 11 == 0 || t._2 % 11 == 0).map(t => t._1 * t._2).filter(n => n.toString == n.toString.reverse).max
res9: Int = 9009
 
scala>
 
scala> (100 to 999).
     |     map(i => (i to 999).
     |     map(j => (i, j))).
     |     flatten.
     |     filter(t => t._1 % 11 == 0 || t._2 % 11 == 0).
     |     map(t => t._1 * t._2).
     |     filter(n => n.toString == n.toString.reverse).
     |     max
res10: Int = 906609

scala>