Skip to content

Instantly share code, notes, and snippets.

@john9631
Last active December 26, 2015 07:19
Show Gist options
  • Save john9631/7114873 to your computer and use it in GitHub Desktop.
Save john9631/7114873 to your computer and use it in GitHub Desktop.
def fib(n):
""" old fib exponential complexity ? """
if n == 0 or n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
#Julia
fib(n::Integer) = return(n > 1 ? (fib(n-1) + fib(n-2)) : one(n))
# ::Integer defines a function for integers only
# Julia's multiple dispatch lets you define methods for different
# types matching against all unnamed types in the method definition
# As suggested by Steven Johnson, using one(n) rather than 1
# ensures that the result has the same type as n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment