<jupyter_start><jupyter_text>---layout: posttitle: Project Euler - Problem 9post-order: 009--- Description of the problem  Solution The first thing to do is to develop an algorithm that XXX. We'll test it out by producing the first 5 pythagorean triples, being $n$ the number of pythagorean triples produced.<jupyter_code>n = 5 # number of triples shown
def first_n_triples(n):
    i = 1 # counter of triplets
    c = 1 # c^2  = a^2 + b^2
    list_of_triples = []

    while i <= n:
        b = c-1
        while b > 1:
            a = b-1
            while a > 1:
                if (c**2 == a**2 + b**2):
                    list_of_triples.append([c, b, a])
                    i += 1
                a -= 1
            b -= 1
        c += 1    
    return list_of_triples
first_n_triples(n)<jupyter_output><empty_output><jupyter_text>It may be the case that the algorithm shows more than the number asked. That may happen because when the largest number $c$ is being increased one by one, in each iteration where a triple is found, the algorithm shows all possible triples with that specific value of $c$. For example: if we want the first five triples, we get the result shown above, whereas for $n=7$ we actually get 8 triples (note that for the last two, $c$ is equal to $25$).<jupyter_code>first_n_triples(7)<jupyter_output><empty_output><jupyter_text>But that won't be a problem to our algorithm/solution. Let's now get the first 500 triples:<jupyter_code>list = first_n_triples(n)<jupyter_output><empty_output><jupyter_text>Here are the first 5 triples...<jupyter_code>list[0:5]<jupyter_output><empty_output><jupyter_text>and the last five in order to not make this post so looooong. We can verify by the last triples found that the triple whose sum adds up to 1000 is in the middle of our setXXX.<jupyter_code>list[n-5:n]<jupyter_output><empty_output><jupyter_text>One of these triples has a sum equal to 1000. Let's find it. We'll run through each XXsublistXX until we find one that gives us a sum which is 1000. After that, We'll show it. On a later step, we'll calculate their product.<jupyter_code>sum = 30
i = 1

while i < n:
    if (list[i][0]+list[i][1]+list[i][2]) == sum:
        print(list[i])
        break
    i += 1
list[i][0]*list[i][1]*list[i][2]
sum = 1000
i = 1

while i < n:
    if (list[i][0]+list[i][1]+list[i][2]) == sum:
        print(list[i])
        break
    i += 1
list[i][0]*list[i][1]*list[i][2]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1075487012987013, 'fraction_numerical': 0.027597402597402596, 'mean_word_length': 3.2869565217391306, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '263564', 'license': 'MIT', 'n_tokens_mistral': 880, 'n_tokens_neox': 818, 'n_words': 302, 'path': '_posts/Problem-009.ipynb', 'repo_name': 'bru1987/euler', 'size': '2464'}