diff --git a/README.markdown b/README.markdown index 9ec9381..64d3c44 100644 --- a/README.markdown +++ b/README.markdown @@ -66,13 +66,13 @@ So you may find some of the code here quite ugly. And this is the case :). Why o 50 - Which prime, below one-million, can be written as the sum of the most consecutive primes? - **~= 1 hour**
52 - Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. - 2min
53 - How many values of C(n,r), for 1 <= n <= 100, exceed one-million? - < 1 sec
+55 - How many Lychrel numbers are there below ten-thousand? - < 1 sec
56 - Considering natural numbers of the form, a^b, finding the maximum digital sum. > 3 sec
67 - Using an efficient algorithm find the maximal sum in the triangle? - 0.027
## In progress: 47 - Find the first four consecutive integers to have four distinct primes factors.
-55 - How many Lychrel numbers are there below ten-thousand?
97 - Find the last ten digits of the non-Mersenne prime: 28433 � 2^7830457 + 1.
## Contact diff --git a/e_55.py b/e_55.py index 33bc5a9..6781794 100644 --- a/e_55.py +++ b/e_55.py @@ -41,10 +41,17 @@ the theoretical nature of Lychrel numbers. ##--- """ +def is_palindromic(num): + """ + Returns True if num is palindromic + Ex : 121 + """ + return (num == reverse(num)) def reverse(num): """ - Returns the palidromic number of num + Returns the reverse number of num + ex : 123 returns 321 """ str_num = str(num) rev = '' @@ -54,6 +61,22 @@ def reverse(num): return int(rev) +def is_lychrel(num, max_it=50): + """ + Given a maximu number of iterations, + returns True if num is a lychrel number + """ + cur = num + it = 1 + while it < max_it: + cur = cur + reverse(cur) + if is_palindromic(cur): + return False + it += 1 + + return True + + def count_lychrel(max_num, max_it=50): """ Returns the number of lychrel numbers smaller than max_num @@ -62,11 +85,12 @@ def count_lychrel(max_num, max_it=50): """ lychrels = [] for i in xrange(1, max_num + 1): - cur = i + if (is_lychrel(i, max_it)): + lychrels.append(i) - return 1 + print lychrels + return len(lychrels) if __name__ == '__main__': - print palindrome(12345) - #print "Answer : %d " % (count_lychrel(10000)) + print "Answer : %d " % (count_lychrel(10000))