From 648870e04db6fdc5e1b0ee8ba939bd49ce0f1b64 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Wed, 8 Feb 2012 17:30:56 +0100 Subject: [PATCH] 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 --- README.markdown | 2 +- e_34.py | 51 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/README.markdown b/README.markdown index c361681..3ce220a 100644 --- a/README.markdown +++ b/README.markdown @@ -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
29 - How many distinct terms are in the sequence generated by ab for 2 a 100 and 2 b 100? - < 1 sec
30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. - < 3 sec
+34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. - 30 sec
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933
42 - Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words? - < 1 sec
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec
@@ -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.
28 - What is the sum of both diagonals in a 1001 by 1001 spiral?
-34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits.
35 - How many circular primes are there below one million?
**WARNING : Spoil inside for those who want to solve problems by themselves :)** diff --git a/e_34.py b/e_34.py index 10f5665..f6df66b 100644 --- a/e_34.py +++ b/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 nb_digits - 1 +def fact(value): + """ + Returns value! + """ + if value == 0: + return 1 + return value * fact(value - 1) - return 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()) + # The major problem in there is to find the upper limit. + print "Answer : %d " % (sum_fact())