Skip to content
Browse files

Add python solution to #9

This solution does some redundant computation. I would still like to
write a Python generator that generates only the distinct triplets.
  • Loading branch information...
1 parent eadf113 commit c23c737476cbb9ecc8bc48fd6d34613b04481fc3 @gaconnet committed
Showing with 41 additions and 0 deletions.
  1. +41 −0 python/euler09.py
View
41 python/euler09.py
@@ -0,0 +1,41 @@
+"""
+A Pythagorean triplet is a set of three natural numbers, a b c, for which,
+
+ a^2 + b^2 = c^2
+
+There exists exactly one Pythagorean triplet for which a + b + c = 1000.
+Find the product abc.
+
+"""
+
+"""
+I see two subproblems here:
+ * Generate all distinct sets {a, b, c} where the elements sum to 1000.
+ * Add constraints to the above set generation so that we only look at
+ numbers that are "close" to satisfying the Pythagorean equation.
+
+For now here is a not-quite-distinct approximation to the first subproblem,
+followed by a trivial filter operation.
+
+"""
+def pairs(t):
+ return ((a, t - a) for a in xrange(1, int(t/2) + 1))
+
+
+def triples(t):
+ return ((a, b, c) for a in xrange(1, int(t/3) + 1)
+ for b, c in pairs(t - a)) # XXX still some symmetries here
+
+if __name__ == '__main__':
+ for a, b, c in triples(1000):
+ if a ** 2 + b ** 2 == c ** 2:
+ print a, b, c, a * b * c, c ** 2
+
+ # count redundant triples
+ stats = dict(
+ used=len(list(triples(1000))),
+ needed=len(set(map(frozenset, triples(1000)))),
+ )
+ stats['redundant'] = stats['used'] - stats['needed']
+
+ print "used {used}; needed {needed}; redundant {redundant}".format(**stats)

0 comments on commit c23c737

Please sign in to comment.
Something went wrong with that request. Please try again.