mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 08:41:20 +00:00
Solves Problem 55
This commit is contained in:
@@ -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** <br />
|
||||
52 - Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. - 2min <br />
|
||||
53 - How many values of C(n,r), for 1 <= n <= 100, exceed one-million? - < 1 sec <br />
|
||||
55 - How many Lychrel numbers are there below ten-thousand? - < 1 sec <br />
|
||||
56 - Considering natural numbers of the form, a^b, finding the maximum digital sum. > 3 sec <br />
|
||||
67 - Using an efficient algorithm find the maximal sum in the triangle? - 0.027 <br />
|
||||
|
||||
## In progress:
|
||||
|
||||
47 - Find the first four consecutive integers to have four distinct primes factors. <br />
|
||||
55 - How many Lychrel numbers are there below ten-thousand? <br />
|
||||
97 - Find the last ten digits of the non-Mersenne prime: 28433 <20> 2^7830457 + 1. <br />
|
||||
|
||||
## Contact
|
||||
|
||||
34
e_55.py
34
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))
|
||||
|
||||
Reference in New Issue
Block a user