A
A
AnanasMaks2021-12-05 15:45:35
Python
AnanasMaks, 2021-12-05 15:45:35

Find all Pythagorean triples where all numbers are in the range [1; 5000]?

A Pythagorean triple is a triple of numbers (a, b, c) such that a ≤ b ≤ c and a2+b2=c2. Find all Pythagorean triples where all numbers are in the range [1; 5000]. Write down in your answer the number of matching triples, and then the value c for the triple in which the sum a + b + c is maximum.
Please help, the code is needed in python, in the answer to the task (5681 4988)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vindicar, 2021-12-05
@Vindicar

I don't understand why you do this. It can be much easier.
You can generate a list of squares of numbers: In this case, the index of an element in the list i will always be one less than the number whose square is at index i. Now the problem is reformulated as follows: find all pairs of numbers from this list, the sum of which is also in this list.
squares = [i*i for i in range(1, 5001)]

for a,a2 in enumerate(squares, 1):
  for b,b2 in enumerate(squares[a:], a+1):
    if (a2+b2) in squares:
      c = squares.index(a2+b2) + 1
      print(a,b,c)

It doesn't work very fast, but it works.
EDIT: You can speed up the code drastically if you consider the following: we don't have to look up the sum in the entire list. We know that the sum will be greater than b^2, i.e. will have an index greater than b. We also know that a^2 + b^2 < (a+b)^2, i.e. the sum will have an index less than a+b. From here:
for a,a2 in enumerate(squares, 1):
  for b,b2 in enumerate(squares[a:], a+1):
    if (a2+b2) in squares[b+1:a+b]:
      c = squares.index(a2+b2) + 1
      print(a,b,c)

S
Sergey Pankov, 2021-12-05
@trapwalker

Start with the simplest non-optimized solution:
You need to iterate over all integer A from 1 to 5000. For each such A, you can iterate over all integer B from A+1 to 5000. If the root sum of their squares is an integer, then this is obviously , the Pythagorean triple. But you need C to be less than or equal to 5000.
For optimization, you can take your time to extract the root, but first check it for excess of 5000 * "2. This way you save a little. You can also calculate the right boundary of the inner loop every time, so you you will look for a solution among half the number of options.
I don’t know what the requirements are now for the efficiency of solving problems, but for schoolchildren I would consider such a solution acceptable. And not for students. Although on apiton, such a task will be considered from a dozen seconds without optimizations. However, time limits were not announced. Commercials and the Monte Carlo method can be solved =)

A
Alexandroppolus, 2021-12-05
@Alexandroppolus

Wikipedia has an article about Pythagorean triples.
there is a "Euclidean formula".
using it, you can quickly and mercilessly generate primitive triples, which are then further multiplied by 2, 3, 4,...
https://ru.wikipedia.org/wiki/%D0%9F%D0%B8%D1%84%D ...

A
alexbprofit, 2021-12-05
@alexbprofit

# Брютфорс, найдите решение пооптимальнее
def get_all_thriads(size):
  res = []
  for i in range(1, size):
    for j in range(1, size):
      for k in range(1, size):
        if i**2 + j**2 == k**2:
          res.append([i, j, k])
  return res

# Тест (возможно наличие одинаковых троек но в разном порядке)
x = [print(el) for el in get_all_thriads(100)]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question