fork download
  1. # Pythagorean_triads (c) 2026 Baltasar MIT License <jbgarcia@uvigo.es>
  2.  
  3.  
  4. from random import randint as rnd
  5. import random
  6.  
  7.  
  8. def crea_random_triads(l, max):
  9. """Creates a list of random triads.
  10. :param l: the list in which to add the triads.
  11. :param max: the number of triads, and each number upper limit.
  12. :return: a list with max triads added.
  13. """
  14. for _ in range(max):
  15. l.append(tuple([rnd(1, max), rnd(1, max), rnd(1, max)]))
  16. ...
  17. ...
  18.  
  19.  
  20. def crea_random_triad(max):
  21. """Creates a single random triad.
  22. :param max: the upper limit for each number.
  23. :return: a tuple.
  24. """
  25. return tuple([rnd(1, max), rnd(1, max), rnd(1, max)])
  26. ...
  27.  
  28.  
  29. def crea_semi_random_triads(l, max):
  30. """Creates a list of random triads, using Euclides' algorithm.
  31. :param l: the list in which to add the triads.
  32. :param max: the number of triads, and each number upper limit.
  33. :return: a list with max triads added.
  34. """
  35. for _ in range(max):
  36. a = -1
  37. b = 0
  38.  
  39. while a < b:
  40. a = rnd(1, max)
  41. b = rnd(1, max)
  42. ...
  43.  
  44. x = (a ** 2) - (b ** 2)
  45. y = 2 * a * b
  46. z = (a ** 2) + (b ** 2)
  47. l.append(tuple([x, y, z]))
  48. ...
  49. ...
  50.  
  51.  
  52. def filter_real_triads(l):
  53. """Filters which triads are pythagorean.
  54. :return: a list with the pythagorean triads in l.
  55. """
  56. return list(filter(lambda t: ((t[0] ** 2) + (t[1] ** 2) == (t[2] ** 2)), l))
  57. ...
  58.  
  59.  
  60. if __name__ == "__main__":
  61. max = 100
  62. random.seed()
  63.  
  64. print("\n# Random triads:")
  65. l1 = [(3, 4, 5)]
  66. pl1 = list(l1)
  67. repetitions = 0
  68. while repetitions < max**2:
  69. t = crea_random_triad(max)
  70. l1.append(t)
  71. if len(filter_real_triads([t])) > 0:
  72. pl1.append(t)
  73. ...
  74. repetitions += 1
  75. ...
  76. print(l1[:20], "...")
  77. print("\n# Pythagorean triads in random list")
  78. print(f"{pl1}\nPercentage: {(len(pl1)/len(l1)) * 100:05.2f}%")
  79. print(f"Repetitions: {repetitions}")
  80.  
  81. print("\n# Semi-random triads:")
  82. l2 = [(3, 4, 5)]
  83. crea_semi_random_triads(l2, max)
  84. print(l2)
  85.  
  86. print("\n# Pythagorean triads in semi-random list")
  87. pl2 = filter_real_triads(l2)
  88. print(f"{pl2}\nPercentage: {(len(pl2)/len(l2)) * 100:05.2f}%")
  89. ...
  90.  
Success #stdin #stdout 0.2s 14264KB
stdin
Standard input is empty
stdout
# Random triads:
[(3, 4, 5), (73, 36, 38), (31, 44, 17), (2, 10, 44), (100, 48, 57), (13, 40, 72), (75, 49, 34), (9, 50, 100), (14, 6, 95), (24, 90, 2), (60, 15, 77), (59, 50, 92), (56, 82, 57), (70, 75, 3), (10, 65, 16), (48, 35, 99), (12, 96, 61), (9, 32, 91), (23, 30, 29), (48, 34, 75)] ...

# Pythagorean triads in random list
[(3, 4, 5), (42, 40, 58), (48, 64, 80)]
Percentage: 00.03%
Repetitions: 10000

# Semi-random triads:
[(3, 4, 5), (7685, 9108, 11917), (3096, 14630, 14954), (1960, 2058, 2842), (4104, 5850, 7146), (4160, 1848, 4552), (1860, 1472, 2372), (7865, 8712, 11737), (8181, 1820, 8381), (1632, 2024, 2600), (2553, 1696, 3065), (6519, 11800, 13481), (4368, 2176, 4880), (2516, 2160, 3316), (791, 6360, 6409), (4727, 12864, 13705), (1404, 1120, 1796), (1752, 10586, 10730), (1917, 2156, 2885), (4331, 660, 4381), (1349, 2340, 2701), (5355, 1628, 5597), (3696, 610, 3746), (105, 608, 617), (1760, 2958, 3442), (1887, 616, 1985), (480, 3182, 3218), (3311, 2040, 3889), (351, 6840, 6849), (780, 8432, 8468), (901, 1260, 1549), (4756, 1680, 5044), (1248, 1064, 1640), (4615, 408, 4633), (3268, 2976, 4420), (3339, 580, 3389), (6313, 3984, 7465), (3749, 13020, 13549), (1287, 6784, 6905), (7384, 8730, 11434), (2400, 1000, 2600), (9600, 392, 9608), (996, 13760, 13796), (3861, 9860, 10589), (7100, 8832, 11332), (6072, 7654, 9770), (2405, 17028, 17197), (192, 494, 530), (768, 1024, 1280), (1127, 936, 1465), (5995, 4428, 7453), (8064, 3680, 8864), (3325, 3900, 5125), (33, 56, 65), (5425, 11400, 12625), (4128, 14504, 15080), (5040, 2482, 5618), (4144, 10560, 11344), (3955, 5772, 6997), (3135, 112, 3137), (1988, 9984, 10180), (2911, 1680, 3361), (1008, 3456, 3600), (1155, 2852, 3077), (1240, 7638, 7738), (5481, 1800, 5769), (212, 5616, 5620), (1749, 860, 1949), (207, 224, 305), (3015, 1232, 3257), (3503, 5904, 6865), (1725, 6500, 6725), (6545, 648, 6577), (6815, 9408, 11617), (319, 360, 481), (2185, 6432, 6793), (3627, 3564, 5085), (2257, 1176, 2545), (4224, 3640, 5576), (3737, 4416, 5785), (1120, 9768, 9832), (4345, 1608, 4633), (3483, 7956, 8685), (528, 896, 1040), (2220, 2288, 3188), (3224, 570, 3274), (7975, 9000, 12025), (6885, 332, 6893), (7384, 8730, 11434), (3840, 12512, 13088), (2124, 6800, 7124), (1155, 2852, 3077), (573, 18236, 18245), (588, 784, 980), (5120, 1152, 5248), (1440, 3038, 3362), (1728, 5704, 5960), (3712, 366, 3730), (1600, 738, 1762), (1261, 4620, 4789), (3483, 2356, 4205)]

# Pythagorean triads in semi-random list
[(3, 4, 5), (7685, 9108, 11917), (3096, 14630, 14954), (1960, 2058, 2842), (4104, 5850, 7146), (4160, 1848, 4552), (1860, 1472, 2372), (7865, 8712, 11737), (8181, 1820, 8381), (1632, 2024, 2600), (2553, 1696, 3065), (6519, 11800, 13481), (4368, 2176, 4880), (2516, 2160, 3316), (791, 6360, 6409), (4727, 12864, 13705), (1404, 1120, 1796), (1752, 10586, 10730), (1917, 2156, 2885), (4331, 660, 4381), (1349, 2340, 2701), (5355, 1628, 5597), (3696, 610, 3746), (105, 608, 617), (1760, 2958, 3442), (1887, 616, 1985), (480, 3182, 3218), (3311, 2040, 3889), (351, 6840, 6849), (780, 8432, 8468), (901, 1260, 1549), (4756, 1680, 5044), (1248, 1064, 1640), (4615, 408, 4633), (3268, 2976, 4420), (3339, 580, 3389), (6313, 3984, 7465), (3749, 13020, 13549), (1287, 6784, 6905), (7384, 8730, 11434), (2400, 1000, 2600), (9600, 392, 9608), (996, 13760, 13796), (3861, 9860, 10589), (7100, 8832, 11332), (6072, 7654, 9770), (2405, 17028, 17197), (192, 494, 530), (768, 1024, 1280), (1127, 936, 1465), (5995, 4428, 7453), (8064, 3680, 8864), (3325, 3900, 5125), (33, 56, 65), (5425, 11400, 12625), (4128, 14504, 15080), (5040, 2482, 5618), (4144, 10560, 11344), (3955, 5772, 6997), (3135, 112, 3137), (1988, 9984, 10180), (2911, 1680, 3361), (1008, 3456, 3600), (1155, 2852, 3077), (1240, 7638, 7738), (5481, 1800, 5769), (212, 5616, 5620), (1749, 860, 1949), (207, 224, 305), (3015, 1232, 3257), (3503, 5904, 6865), (1725, 6500, 6725), (6545, 648, 6577), (6815, 9408, 11617), (319, 360, 481), (2185, 6432, 6793), (3627, 3564, 5085), (2257, 1176, 2545), (4224, 3640, 5576), (3737, 4416, 5785), (1120, 9768, 9832), (4345, 1608, 4633), (3483, 7956, 8685), (528, 896, 1040), (2220, 2288, 3188), (3224, 570, 3274), (7975, 9000, 12025), (6885, 332, 6893), (7384, 8730, 11434), (3840, 12512, 13088), (2124, 6800, 7124), (1155, 2852, 3077), (573, 18236, 18245), (588, 784, 980), (5120, 1152, 5248), (1440, 3038, 3362), (1728, 5704, 5960), (3712, 366, 3730), (1600, 738, 1762), (1261, 4620, 4789), (3483, 2356, 4205)]
Percentage: 100.00%