Skip to content

Instantly share code, notes, and snippets.

@genos
Created October 2, 2013 19:13
Show Gist options
  • Save genos/6799000 to your computer and use it in GitHub Desktop.
Save genos/6799000 to your computer and use it in GitHub Desktop.
A Python file to play with a special continued fraction. If you don't have a Python interpreter, try running it in an online one (copy and paste the code), like: http://repl.it/ http://codepad.org/ http://labs.codecademy.com/
def x(k):
"""Numerically investigate the continued fraction x =
1 + 1
________________
1 + 1
__________
1 + 1
_____
...
We won't be able to go out to infinity, so we'll use a counter to take only
a (large) finite number of steps down this fraction.
This is a recursive function, in that it will call itself until a certain
stopping criterion is met. If our counter k is less than 1, we'll return
1; this is the stopping criterion---or base case---of our recursion. If
not, we'll return 1 + 1 / x(k - 1) (our recursive step).
"""
if k <= 1:
return 1.0
else:
return 1.0 + 1.0 / x(k - 1)
# Now we'll see this in action
if __name__ == '__main__':
# Print out 250 steps of our continued fraction
print(x(250)) # Does the output look familiar?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment