(Finally) solves problem 10.

The code is slow !

Prepares problem 11
This commit is contained in:
Julien Lengrand-Lambert
2012-01-13 15:26:24 +01:00
parent 9c011f1d56
commit afcf2ab0c5
3 changed files with 72 additions and 2 deletions

17
e_10.py
View File

@@ -13,8 +13,21 @@ def sum_primes(value):
""" """
Returns the sum of prime number below 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])
return 1 curr_val +=1
ptr = False
return sum(prime_list)
if __name__ == '__main__': if __name__ == '__main__':
print "Answer : %d" % (sum_primes()) print "Answer : %d" % (sum_primes(2000000))

37
e_10_nope.py Executable file
View File

@@ -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))

20
e_11.py Executable file
View File

@@ -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))