如何使用递归在 Swift Playground 中打印斐波那契数列
How to print the Fibonacci sequence in Swift Playground using recursion
我正在尝试在 Swift 中使用递归来打印出 "n" 次迭代的斐波那契数列。但是,我一直收到同样的错误。
我已经尝试过不使用递归并且能够做到。但是,我现在正尝试通过使用递归以更复杂和 "computer scientisty" 的方式来做。
func fibonacciSequence (n: Int) -> [Int] {
// Consumes a number "n", which is the number of iterations to go through with the Fibonacci formula and prints such sequence.
var fibonacciArray = [Int]()
for n in 0 ... n {
if n == 0 {
fibonacciArray.append(0)
}
else if n == 1 {
fibonacciArray.append(1)
}
else {
fibonacciArray.append (fibonacciSequence(n: (n - 1)) +
fibonacciSequence(n: (n-2)))
}
}
return fibonacciArray
我希望用数字 n 调用该函数,并希望该函数打印出斐波那契数列。示例:如果 n = 5,我希望控制台打印 0、1、1、2、3、5。我得到的错误是:(无法将类型 '[Int]' 的值转换为预期的参数类型 'Int').
如上所述,return 值在求和时会导致错误。修复代码的一种可能方法(但不是递归的)是简单地更改 else
语句:
func fibonacciSequence (n: Int) -> [Int] {
// Consumes a number "n", which is the number of iterations to go through with the Fibonacci formula and prints such sequence.
var fibonacciArray = [Int]()
for n in 0 ... n {
if n == 0 {
fibonacciArray.append(0)
}
else if n == 1 {
fibonacciArray.append(1)
}
else {
fibonacciArray.append (fibonacciArray[n-1] + fibonacciArray[n-2] )
}
}
return fibonacciArray
}
递归解决方案如下:
func fibonacciSequence (n: Int, sumOne: Int, sumTwo: Int, counter: Int, start: Bool) {
if start {
print(0)
print(1)
}
if counter == -1 {
print(1)
}
if (counter == n - 2) {
return
}
let sum = sumOne + sumTwo
print(sum)
fibonacciSequence(n: n, sumOne: sumTwo , sumTwo: sum, counter: counter + 1, start: false)
}
fibonacciSequence(n: 8, sumOne: 0, sumTwo: 1, counter: 0, start: true)
可能有一个 "nicer" 方法,但我希望它能有所帮助。干杯。
fabonacci的递归方式->解法
func fibo( n: Int) -> Int {
guard n > 1 else { return n }
return fibo(n: n-1) + fibo(n: n-2)
}
这些是我在 swift 5 playground
中斐波那契数列的解决方案
func fibonacci(n: Int) {
var num1 = 0
var num2 = 1
var nextNum = Int()
let i = 1
var array = [Int]()
array.append(num1)
array.append(num2)
for _ in i...n {
nextNum = num1 + num2
num1 = num2
num2 = nextNum
array.append(num2)
print(array)
}
print("result = \(num2)")
}
打印(斐波那契(n:5))
let fibonacci = sequence(state: (0, 1)) {(state: inout (Int, Int)) -> Int? in
defer { state = (state.1, state.0 + state.1) }
return state.0
}
//limit 10
for number in fibonacci.prefix(10) {
print(number)
}
// 标记:- 函数
func fibonacciSeries(_ num1 : Int,_ num2 : Int,_ term : Int,_ termCount : Int) -> Void{
if termCount != term{
print(num1)
fibonacciSeries(num2, num2+num1, term, termCount + 1)
}
}
// MARK: - 调用函数 fibonacciSeries(0, 1, 5, 0)
// MARK: - 输出 Put 0 1 1 2 3
注意只需要更改斐波那契数列的项数。
func fibonacci(n: Int) {
var seq: [Int] = n == 0 ? [0] : [0, 1]
var curNum = 2
while curNum < n{
seq.append(seq[curNum - 1] + seq[curNum - 2])
curNum += 1 }
print(seq) }
我正在尝试在 Swift 中使用递归来打印出 "n" 次迭代的斐波那契数列。但是,我一直收到同样的错误。
我已经尝试过不使用递归并且能够做到。但是,我现在正尝试通过使用递归以更复杂和 "computer scientisty" 的方式来做。
func fibonacciSequence (n: Int) -> [Int] {
// Consumes a number "n", which is the number of iterations to go through with the Fibonacci formula and prints such sequence.
var fibonacciArray = [Int]()
for n in 0 ... n {
if n == 0 {
fibonacciArray.append(0)
}
else if n == 1 {
fibonacciArray.append(1)
}
else {
fibonacciArray.append (fibonacciSequence(n: (n - 1)) +
fibonacciSequence(n: (n-2)))
}
}
return fibonacciArray
我希望用数字 n 调用该函数,并希望该函数打印出斐波那契数列。示例:如果 n = 5,我希望控制台打印 0、1、1、2、3、5。我得到的错误是:(无法将类型 '[Int]' 的值转换为预期的参数类型 'Int').
如上所述,return 值在求和时会导致错误。修复代码的一种可能方法(但不是递归的)是简单地更改 else
语句:
func fibonacciSequence (n: Int) -> [Int] {
// Consumes a number "n", which is the number of iterations to go through with the Fibonacci formula and prints such sequence.
var fibonacciArray = [Int]()
for n in 0 ... n {
if n == 0 {
fibonacciArray.append(0)
}
else if n == 1 {
fibonacciArray.append(1)
}
else {
fibonacciArray.append (fibonacciArray[n-1] + fibonacciArray[n-2] )
}
}
return fibonacciArray
}
递归解决方案如下:
func fibonacciSequence (n: Int, sumOne: Int, sumTwo: Int, counter: Int, start: Bool) {
if start {
print(0)
print(1)
}
if counter == -1 {
print(1)
}
if (counter == n - 2) {
return
}
let sum = sumOne + sumTwo
print(sum)
fibonacciSequence(n: n, sumOne: sumTwo , sumTwo: sum, counter: counter + 1, start: false)
}
fibonacciSequence(n: 8, sumOne: 0, sumTwo: 1, counter: 0, start: true)
可能有一个 "nicer" 方法,但我希望它能有所帮助。干杯。
fabonacci的递归方式->解法
func fibo( n: Int) -> Int {
guard n > 1 else { return n }
return fibo(n: n-1) + fibo(n: n-2)
}
这些是我在 swift 5 playground
中斐波那契数列的解决方案func fibonacci(n: Int) {
var num1 = 0
var num2 = 1
var nextNum = Int()
let i = 1
var array = [Int]()
array.append(num1)
array.append(num2)
for _ in i...n {
nextNum = num1 + num2
num1 = num2
num2 = nextNum
array.append(num2)
print(array)
}
print("result = \(num2)")
}
打印(斐波那契(n:5))
let fibonacci = sequence(state: (0, 1)) {(state: inout (Int, Int)) -> Int? in
defer { state = (state.1, state.0 + state.1) }
return state.0
}
//limit 10
for number in fibonacci.prefix(10) {
print(number)
}
// 标记:- 函数
func fibonacciSeries(_ num1 : Int,_ num2 : Int,_ term : Int,_ termCount : Int) -> Void{
if termCount != term{
print(num1)
fibonacciSeries(num2, num2+num1, term, termCount + 1)
}
}
// MARK: - 调用函数 fibonacciSeries(0, 1, 5, 0)
// MARK: - 输出 Put 0 1 1 2 3
注意只需要更改斐波那契数列的项数。
func fibonacci(n: Int) {
var seq: [Int] = n == 0 ? [0] : [0, 1]
var curNum = 2
while curNum < n{
seq.append(seq[curNum - 1] + seq[curNum - 2])
curNum += 1 }
print(seq) }