Skip to content

Instantly share code, notes, and snippets.

@islandjoe
Created June 10, 2018 17:17
Show Gist options
  • Save islandjoe/42aa5c293c0340865af0c56a8d8c9421 to your computer and use it in GitHub Desktop.
Save islandjoe/42aa5c293c0340865af0c56a8d8c9421 to your computer and use it in GitHub Desktop.
A performant Fibonacci function. Much preferable than the recursive version.
func fibn(n: UInt) -> UInt {
  if n == 0 {
    return n
  }
  
  var last: UInt = 0, 
      next: UInt = 1
  
  for _ in 1..<n {
    (last, next) = (next, last+next)
  }
  
  return next
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment