mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 08:41:20 +00:00
Solves problem 34. Average running time, could be optimized with
memoization Still have to perform some good work on prime numbers! Signed-off-by: Julien Lengrand-Lambert <julien@lengrand.fr>
This commit is contained in:
@@ -41,6 +41,7 @@ Should be used in order to help future reuse of code :)
|
||||
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 />
|
||||
30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. - < 3 sec <br />
|
||||
34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. - 30 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 />
|
||||
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 />
|
||||
@@ -52,7 +53,6 @@ Should be used in order to help future reuse of code :)
|
||||
|
||||
26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle. <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 />
|
||||
|
||||
**WARNING : Spoil inside for those who want to solve problems by themselves :)**
|
||||
|
||||
49
e_34.py
49
e_34.py
@@ -11,14 +11,55 @@ Find the sum of all numbers which are equal to the sum of the factorial of their
|
||||
|
||||
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
|
||||
'''
|
||||
def sum_power():
|
||||
def find_limit():
|
||||
"""
|
||||
Finds
|
||||
Returns the max number of digits we have to loop through to find the solution
|
||||
"""
|
||||
nb_digits = 2
|
||||
max_num = fact(9) * nb_digits
|
||||
|
||||
while len(str(max_num)) >= nb_digits:
|
||||
nb_digits += 1
|
||||
|
||||
return 1
|
||||
return nb_digits - 1
|
||||
|
||||
def fact(value):
|
||||
"""
|
||||
Returns value!
|
||||
"""
|
||||
if value == 0:
|
||||
return 1
|
||||
return value * fact(value - 1)
|
||||
|
||||
def digsum(num):
|
||||
"""
|
||||
Returns the sum of factorial of digits of num
|
||||
ex : digsum(123, 4) = fact(1) + fact(2) + fact(3)
|
||||
"""
|
||||
val = 0
|
||||
for el in str(num):
|
||||
val += fact(int(el))
|
||||
|
||||
return val
|
||||
|
||||
def sum_fact():
|
||||
"""
|
||||
Finds the sum of all the numbers that can be written as the sum of the factorial of their digits.
|
||||
"""
|
||||
max_dig = find_limit()
|
||||
max_val = fact(9) * max_dig
|
||||
print "Max dig is : %d" %(max_dig)
|
||||
sumf = 0
|
||||
|
||||
cpt = 3
|
||||
while cpt <= max_val:
|
||||
if cpt == digsum(cpt):
|
||||
sumf += cpt
|
||||
|
||||
cpt +=1
|
||||
|
||||
return sumf
|
||||
|
||||
if __name__ == '__main__':
|
||||
# The major problem in there is to find the upper limit.
|
||||
print "Answer : %d " % (sum_power())
|
||||
print "Answer : %d " % (sum_fact())
|
||||
|
||||
Reference in New Issue
Block a user