Here's the solution using recursion:
main.swift193 chars11 lines
This function takes an integer nth
as input and returns the corresponding Fibonacci number. It uses recursion to calculate the nth Fibonacci number by summing the previous two (n-1 and n-2) Fibonacci numbers. The base case is when nth <= 1
, which returns nth
itself since the Fibonacci sequence starts with 0 and 1.
gistlibby LogSnag