Last active
December 26, 2015 07:19
-
-
Save john9631/7114873 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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