Skip to content

Instantly share code, notes, and snippets.

@dgouldin
Created March 11, 2011 21:02
Show Gist options
  • Save dgouldin/866575 to your computer and use it in GitHub Desktop.
Save dgouldin/866575 to your computer and use it in GitHub Desktop.
My entry to Google's PyCon Quiz: Count and sum all positive palindromic numbers up to a googol
def googol():
def palindromes(end):
for odd_length in (True, False):
left = 0
last_palindrome = 0
while last_palindrome < end:
left += 1
left_str = str(left)
right_str = left_str[::-1]
if odd_length:
left_str = left_str[:-1]
last_palindrome = int('{0}{1}'.format(left_str, right_str))
yield last_palindrome
count_palindromes = 0
sum_palindromes = 0
for palindrome in palindromes(10**100):
count_palindromes += 1
sum_palindromes += palindrome
return (sum_palindromes, count_palindromes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment