From afcf2ab0c578d8fa2a03ed4d1686b90cb5011d85 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Fri, 13 Jan 2012 15:26:24 +0100 Subject: [PATCH] (Finally) solves problem 10. The code is slow ! Prepares problem 11 --- e_10.py | 17 +++++++++++++++-- e_10_nope.py | 37 +++++++++++++++++++++++++++++++++++++ e_11.py | 20 ++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100755 e_10_nope.py create mode 100755 e_11.py diff --git a/e_10.py b/e_10.py index 133fa51..2ef902b 100755 --- a/e_10.py +++ b/e_10.py @@ -13,8 +13,21 @@ def sum_primes(value): """ Returns the sum of prime number below value """ + prime_list = [2] + curr_val = 3 + ptr = False + while( curr_val < value): + for primes in prime_list: + if (curr_val % primes == 0): + ptr = True # multiple of at least one value + if not ptr: + prime_list.append(curr_val) + print "%d%% / %d" % (((prime_list[-1] / float(value)) * 100), prime_list[-1]) + + curr_val +=1 + ptr = False - return 1 + return sum(prime_list) if __name__ == '__main__': - print "Answer : %d" % (sum_primes()) + print "Answer : %d" % (sum_primes(2000000)) diff --git a/e_10_nope.py b/e_10_nope.py new file mode 100755 index 0000000..1b06c74 --- /dev/null +++ b/e_10_nope.py @@ -0,0 +1,37 @@ +#!/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 + """ + prime_list = [2] + curr_val = 3 + ptr = False + while( curr_val <= value): + while(not ptr): + for primes in prime_list: + if primes < pow(curr_val, 0.5): + if (curr_val % primes == 0): + ptr = True # multiple of at least one value + if not ptr: + prime_list.append(curr_val) + print "%d%% / %d" % (((prime_list[-1] / float(value)) * 100), prime_list[-1]) + ptr = True + + curr_val +=1 + ptr = False + + #print prime_list + return sum(prime_list) + +if __name__ == '__main__': + print "Answer : %d" % (sum_primes(20000)) diff --git a/e_11.py b/e_11.py new file mode 100755 index 0000000..cd166df --- /dev/null +++ b/e_11.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +""" + ##--- + # jlengrand + #Created on : Fri Jan 13 15:24:59 CET 2012 + # + # DESCRIPTION : Solves problem 11 of Project Euler + 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. + What is the sum of the digits of the number 2^1000? + ##--- +""" +def sum_power_2(value): + """ + Returns the sum of the digits of 2^value + """ + + return 1 + +if __name__ == '__main__': + print "Answer : %d" % (sum_power_2(1000))