Logo 61e73bc61c0e1780102320e879dc3cac0e29303ea8894dd8f10c942f3ba8120f

otters.io

An adorable little blog

Swift-o-nacci

Friday — January 30th, 2015

I couldn't find a method for getting Fibonacci values in Swift for Project Euler. It probably exists somewhere in some standard library but I couldn't find it through a quick search and I really don't want to rtfm. Here is a tiny Swift function that recursively calculates Fibonacci values.


func fibonacci(n: Int) -> Int {

  if n == 0 || n == 1 { 

    return(n) 

  } else {

    return(fibonacci(n - 1) + fibonacci(n - 2))

  }

}