Recursive Function to generate / print a Fibonacci series

前端 未结 4 523
情书的邮戳
情书的邮戳 2020-12-21 18:55

I am trying to create a recursive function call method that would print the Fibonacci until a specific location:

1 function f = fibonacci(n)
2 fprintf(\'The          


        
4条回答
  •  甜味超标
    2020-12-21 19:26

    Create a function, which returns Integer:

    func fibonacci(number n : Int) -> Int 
    {
        guard n > 1 else {return n}
        return fibonacci(number: n-1) + fibonacci(number: n-2)
    }
    

    This will return the fibonacci output of n numbers, To print the series You can use this function like this in swift:

    for _ in 0...10
    {
        print(fibonacci(number : 10))
    }
    

    It will print the series of 10 numbers.

提交回复
热议问题