Solves problem 30. Good running time

Still have to perform some good work on prime numbers! 

Signed-off-by: Julien Lengrand-Lambert <julien@lengrand.fr>
This commit is contained in:
Julien Lengrand-Lambert
2012-02-08 16:49:39 +01:00
parent a790a6de0e
commit a85994f38a
3 changed files with 66 additions and 7 deletions

View File

@@ -40,6 +40,7 @@ Should be used in order to help future reuse of code :)
25 - What is the first term in the Fibonacci sequence to contain 1000 digits? - 0.741 <br /> 25 - What is the first term in the Fibonacci sequence to contain 1000 digits? - 0.741 <br />
27 - Find a quadratic formula that produces the maximum number of primes for consecutive values of n. - too long <br /> 27 - Find a quadratic formula that produces the maximum number of primes for consecutive values of n. - too long <br />
29 - How many distinct terms are in the sequence generated by ab for 2 a 100 and 2 b 100? - < 1 sec <br /> 29 - How many distinct terms are in the sequence generated by ab for 2 a 100 and 2 b 100? - < 1 sec <br />
30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. - < 3 sec <br />
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933 <br /> 36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933 <br />
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec <br /> 42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec <br />
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec <br /> 45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec <br />
@@ -50,7 +51,8 @@ Should be used in order to help future reuse of code :)
**In progress: ** **In progress: **
26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle. <br /> 26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle. <br />
30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. <br /> 28 - What is the sum of both diagonals in a 1001 by 1001 spiral? <br />
34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. <br />
35 - How many circular primes are there below one million? <br /> 35 - How many circular primes are there below one million? <br />
**WARNING : Spoil inside for those who want to solve problems by themselves :)** **WARNING : Spoil inside for those who want to solve problems by themselves :)**

45
e_30.py
View File

@@ -16,14 +16,47 @@ The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
''' '''
def find_limit(power):
def largest():
""" """
Returns Returns the max number of digits we have to loop through to find the solution, for a given power
""" """
nb_digits = 2
max_num = 9**power * nb_digits
while len(str(max_num)) >= nb_digits:
nb_digits += 1
return nb_digits - 1
def digsum(num, power):
"""
Returns the sum of power of digits of num
ex : digsum(123, 4) = 1**4 + 2**4 + 3**4
"""
val = 0
for el in str(num):
val += int(el) ** power
return val
def sum_power(power):
"""
Finds the sum of all the numbers that can be written as the sum of power powers of their digits.
"""
max_dig = find_limit(power)
max_val = 9**power * max_dig
print "Max dig is : %d" %(max_dig)
sump = 0
cpt = 2
while cpt <= max_val:
if cpt == digsum(cpt, power):
sump += cpt
cpt +=1
return 1 return sump
if __name__ == '__main__': if __name__ == '__main__':
print "Answer : %d " % (largest()) # The major problem in there is to find the upper limit.
print "Answer : %d " % (sum_power(5))

24
e_34.py Normal file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env python
'''
Created on 8 feb. 2012
@author: Julien Lengrand-Lambert
DESCRIPTION: Solves problem 34 of Project Euler
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
'''
def sum_power():
"""
Finds
"""
return 1
if __name__ == '__main__':
# The major problem in there is to find the upper limit.
print "Answer : %d " % (sum_power())