Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Special Pythagorean triplet : Python Code : Euler Project : Problem 9

Saturday, August 30, 2014

Special Pythagorean triplet : Python Code : Euler Project : Problem 9

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Problem Source: Euler Project

Python Code

def specialPythagoreanTriplet():
    product = 1
    flag = False
    for  a in range (1000):
        a = a+1
        for b in range(1000):
            b = b+1
            if a+b > 1000 or a == b:
                continue
            for c in range(1000):
                c = c+1
                if a+b+c != 1000 or c%4 == 2 or c%4 == 3:
                    continue
                if a**2+b**2 == c**2 and a+b+c == 1000:
                    product = a*b*c   
                    print a,b,c
                    flag = True
                    break
            if flag == True:
                break       
        if flag == True:
            break       
    print "The prouct abc:",product       
specialPythagoreanTriplet()

No comments:

Post a Comment