Skip to content

Instantly share code, notes, and snippets.

@jeremyBanks
Created July 31, 2008 00:12
Show Gist options
  • Save jeremyBanks/3360 to your computer and use it in GitHub Desktop.
Save jeremyBanks/3360 to your computer and use it in GitHub Desktop.
Fibbonachi generator for the Reddit thread.
#!/usr/bin/env python
import sys
import re
def prompt():
sys.stderr.write("Preceding term: (terminate with EOF)\n")
return sys.stdin.read()
def main():
predecessor = int(re.sub("[^\+\-eE0-9]", "", "".join(sys.argv[1:]) if len(sys.argv) > 1 else prompt()) or 0)
n = 1
previous = 0
current = 1
while current <= predecessor:
n += 1
previous, current = current, previous + current
if previous != predecessor:
sys.stderr.write("Warning: Given predeccessor is not in the Fibbonachi sequence!\n")
wrap = 48
lead = "fib(%i) = " % n
number = len(lead) * " " + "%i" % current
string = lead + " " * (wrap - len(lead)) + number
# Wrap lines to {wrap} chars, crudely.
formatted = "\t" + "\n\t".join(string[i:i+wrap] for i in range(0, len(string) + 1, 48))
print formatted
if __name__ == "__main__": sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment