Last active
June 12, 2016 05:07
-
-
Save apphands/64ee53aadf04af68ed4d15a20bca48c9 to your computer and use it in GitHub Desktop.
Calculate Fibonacci numbers in Swift
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
// https://en.wikipedia.org/wiki/Fibonacci_number | |
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ... | |
func fib(n:Int)->Int { | |
if n > 2 { | |
return fib(n-1) + fib(n-2) | |
} else { | |
return n | |
} | |
} | |
//usage | |
fib(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment