Processing math: 0%
≡ Menu

Project Euler: Problem 9

That is the problem:

We can solve the problem by writing a Python code.

Let’s start with a function that checks if the three numbers a,b,c, with a<b<c, are a Pythagorean triplet.

1
2
3
4
5
def is_triplet(a,b,c):
    if (a**2 + b**2) == c**2:
        return(True)
    else:
        return(False)

Now, let’s move on to defining the main function:

1
2
3
4
5
6
7
8
def main(sum_value):
    for c in range(sum_value):
        for b in range(c):
            for a in range (b):
                if (a+b+c) == sum_value:
                    if is_triplet(a,b,c):
                        print(a,b,c)
                        print(a*b*c)

and then, the main program:

1
2
sum_total = 1000
main(sum_total)

This is the answer:

{ 0 comments… add one }

Rispondi

Next post:

Previous post: