diff --git a/README.markdown b/README.markdown index 5800b8a..c361681 100644 --- a/README.markdown +++ b/README.markdown @@ -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
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
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
@@ -50,7 +51,8 @@ Should be used in order to help future reuse of code :) **In progress: ** 26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle.
-30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
+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_30.py b/e_30.py index e1c46c3..ebd69c5 100644 --- a/e_30.py +++ b/e_30.py @@ -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. ''' - - -def largest(): +def find_limit(power): """ - 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__': - print "Answer : %d " % (largest()) + # The major problem in there is to find the upper limit. + print "Answer : %d " % (sum_power(5)) diff --git a/e_34.py b/e_34.py new file mode 100644 index 0000000..10f5665 --- /dev/null +++ b/e_34.py @@ -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())