Created
September 20, 2010 00:25
-
-
Save duskcheetah/587263 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# prints lychrel candidate numbers up to 10000 | |
lychrels = set() | |
not_lychrels = set() | |
seeds = set() | |
def is_palindrome(n): | |
n_str = str(n) | |
if n_str[::-1] == n_str: | |
return True | |
else: | |
return False | |
def test_lychrel(n, rem_generations, first): | |
if rem_generations == 0: | |
return True | |
if n in lychrels: | |
return True | |
if n in not_lychrels: | |
return False | |
if is_palindrome(n) and first == False: | |
not_lychrels.add(n) | |
return False | |
rev = int(str(n)[::-1]) | |
next = n + rev | |
is_lychrel = test_lychrel(next, rem_generations - 1, False) | |
if is_lychrel == False: | |
not_lychrels.add(n) | |
return False | |
if is_lychrel == True: | |
if n < 10000: | |
lychrels.add(n) | |
return True | |
for i in range(10000): | |
test_lychrel(i, 40, True) | |
lychrel_list = list(lychrels) | |
lychrel_list.sort() | |
print "Lychrels: ", lychrel_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment