Created
December 16, 2011 18:53
-
-
Save jakedobkin/1487384 to your computer and use it in GitHub Desktop.
Euler 55: Lychrel numbers
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
| # http://projecteuler.net/problem=56 | |
| # first, a few functions we'll use- i particularly like the reverse | |
| def reverse(string): | |
| string = string[::-1] | |
| return string | |
| def is_pali(string): | |
| if string==reverse(string): | |
| return True | |
| else: | |
| return False | |
| # is_lychrel is a recursive function! i wrote it without any help! | |
| def is_lychrel(x,y): | |
| sum = x + int(reverse(str(x))) | |
| if is_pali(str(sum)) == True: | |
| return False | |
| if y > 50: | |
| return True | |
| else: | |
| y = y + 1 | |
| return is_lychrel(sum,y) | |
| # now we simply ask if each number is lychrel and count | |
| count = 0 | |
| for i in range (0,10000): | |
| if is_lychrel(i,0): | |
| count += 1 | |
| print "Answer:",count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment