Skip to content

Instantly share code, notes, and snippets.

@christianp
Last active December 20, 2015 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianp/6079355 to your computer and use it in GitHub Desktop.
Save christianp/6079355 to your computer and use it in GitHub Desktop.
Palindromic base-fibonacci numbers
#generate palindromic base-fibonacci numbers
#palindromes have to start and end with a 1!
from itertools import count
def fib_palindrome_of_length(n):
"""
Yields all palindromic base-fibonacci numbers of length n
"""
if n==0:
yield ''
elif n==1:
yield '0'
yield '1'
elif n==2:
yield '00'
elif n==3:
yield '000'
yield '010'
yield '101'
else:
for b in fib_palindrome_of_length(n-2):
yield '0'+b+'0'
for a in fib_palindrome_of_length(n-4):
yield '10'+a+'01'
def fib_palindromes():
for l in count(1):
for a in fib_palindrome_of_length(l):
if a[0]=='1':
yield a
def fibs():
a=b=1
while True:
a,b=b,a+b
yield a
def fib_to_decimal(n):
total = 0
for bit,worth in zip(n,fibs()):
if bit=='1':
total+=worth
return total
s=', '.join(str(fib_to_decimal(a)) for i,a in zip(range(100),fib_palindromes()))
print(s)
1, 4, 6, 9, 12, 14, 22, 27, 33, 35, 51, 56, 64, 74, 80, 88, 90, 116, 127, 145, 158, 174, 184, 197, 203, 216, 232, 234, 276, 294, 326, 368, 378, 399, 425, 441, 462, 472, 493, 519, 525, 546, 572, 588, 609, 611, 679, 708, 760, 828, 847, 915, 944, 988, 1022, 1064, 1090, 1124, 1140, 1174, 1216, 1226, 1260, 1302, 1328, 1362, 1368, 1402, 1444, 1470, 1504, 1520, 1554, 1596, 1598, 1708, 1755, 1839, 1949, 1980, 2090, 2137, 2211, 2321, 2368, 2452, 2562, 2585, 2640, 2708, 2750, 2805, 2831, 2886, 2954, 2970, 3025, 3093, 3135, 3190
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment