From 9c011f1d56f8b46935f53911ac39362e551519c9 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Fri, 13 Jan 2012 11:43:29 +0100 Subject: [PATCH] Solves Problems 9 Prepares problem 10 --- e_10.py | 20 ++++++++++++++++++++ e_9.py | 29 ++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100755 e_10.py diff --git a/e_10.py b/e_10.py new file mode 100755 index 0000000..133fa51 --- /dev/null +++ b/e_10.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +""" + ##--- + # jlengrand + #Created on : Fri Jan 13 11:42:09 CET 2012 + # + # DESCRIPTION : Solves problem 10 of Project Euler + The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + Find the sum of all the primes below two million. + ##--- +""" +def sum_primes(value): + """ + Returns the sum of prime number below value + """ + + return 1 + +if __name__ == '__main__': + print "Answer : %d" % (sum_primes()) diff --git a/e_9.py b/e_9.py index 02763cb..1956172 100755 --- a/e_9.py +++ b/e_9.py @@ -14,11 +14,34 @@ Find the product abc. ##--- """ -def fun(): +def pythagorean_triplet(value): """ + Returns the product of the Pythagoren triplet for which in addition + a + b + c = value """ + pyth_list = pyth_possibilities(value) + for pyth in pyth_list: + if is_pyth_triplet(pyth): + return pyth[0] * pyth[1] * pyth[2] + return 0 - return 1 +def pyth_possibilities(value): + """ + Creates a list of all triplets wich sum is equal to value + FIXME = Is there a way to avoid redundancies? + """ + out_list = [] + for j in range(1, value - 1): + for k in range(1, value - 1): + out_list.append([j, k, value - j - k]) + return out_list + +def is_pyth_triplet(suite): + """ + Returns true if the suite contains a pythagorean triplet, false otherwise + """ + return pow(suite[0], 2) + pow(suite[1], 2) == pow(suite[2], 2) if __name__ == '__main__': - print "Answer : %d" % (fun()) + val = 1000 + print "Answer : %d" % (pythagorean_triplet(val))