## mapping a given number to its fibonacci code ## reads from stdin and writes binary to stdout (David's version) fibs = [1, 1] def mapcode(n, fibs): if (n==1): return '_' ## extend the fibonacci sequence if necessary while (n>fibs[-1]) : fibs.append(fibs[-1]+fibs[-2]) less = filter((lambda a: a<=n), fibs) ## make list of all _<=_ fibonacci numbers length = len(less) ## find the index assert length>=2 pos = n-less[-1] ## find how far we are above the lower fibonumber ## find the point above which this dude could start with a '1' ## boundary = less[length-1] + less[length-3] if less[-3] <= pos: temp = mapcode(pos,less) temp = '1'+temp return temp pass else: temp = mapcode(pos+less[-2],less) temp = '0'+temp return temp pass pass import sys, string if(0): for line in sys.stdin.readlines(): if line[0] != '#' : words = string.split(line) for a in words : temp = mapcode(int(a), fibs) print temp pass if __name__ == '__main__': for n in range(1,24): print "%3d %s" % (n, mapcode(n,fibs))