The beauty of the functional programming

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 import sys
 import cmath

 def product (a, b, c):
     print a * b * c
     sys.exit ()

 c = lambda a, b: abs (cmath.sqrt (a**2 + b**2))
 f = lambda a, b, c: c % 1 == 0 and a + b + c == 1000
 g = lambda a, b, c: f (a, b, c) and product (a, b, c)

[ (a, b) for a in xrange (1, 1000) for b in xrange (a + 1, 1000) if g (a, b, c (a, b)) ]