Skip to content

Instantly share code, notes, and snippets.

@rickw
Last active November 10, 2017 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickw/846291c707bf02922d60 to your computer and use it in GitHub Desktop.
Save rickw/846291c707bf02922d60 to your computer and use it in GitHub Desktop.
Fibonacci sequence in Swift
func fibber(n:Int) -> Int {
switch n {
case 0:
return 0
case 1:
return 1
default:
return fibber(n-1) + fibber(n-2)
}
}
@TomCarton
Copy link

TomCarton commented Nov 10, 2017

I would have rather done it without recursion (recursion is evil), such as:

func fibo(_ n:Int) -> Int {

    var a = 0
    var b = 1

    for _ in 0..<n {
        a += b
        b = a - b
    }

    return a
}

it is way faster to execute!

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