Skip to content

Instantly share code, notes, and snippets.

@deidyomega
Created March 16, 2016 00:28
Show Gist options
  • Save deidyomega/a579c0af08bf531b07f8 to your computer and use it in GitHub Desktop.
Save deidyomega/a579c0af08bf531b07f8 to your computer and use it in GitHub Desktop.
from decimal import *
import math
def mem_fib(n, _cache={}):
'''efficiently memoized recursive function, returns a Fibonacci number'''
if n in _cache:
return _cache[n]
elif n > 1:
return _cache.setdefault(n, mem_fib(n-1) + mem_fib(n-2))
return n
getcontext().prec = 200
result = Decimal(mem_fib(601)) / Decimal(mem_fib(600))
wiki_golden_ratio = "1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902679"
print result
print wiki_golden_ratio
print str(result) == wiki_golden_ratio
@deidyomega
Copy link
Author

This little test helps prove that the decimal library can handle precision far greater than floating point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment