I need some help with a program I\'m writing for my Programming II class at universtiy. The question asks that one calculates the Fibonacci sequence using recursion. One mus
Might be too old but here is my solution for swift
class Recursion {
func fibonacci(_ input: Int) {
var dictioner: [Int: Int] = [:]
dictioner[0] = 0
dictioner[1] = 1
print(fibonacciCal(input, dictioner: &dictioner))
}
func fibonacciCal(_ input: Int, dictioner: inout [Int: Int]) -> Int {
if let va = dictioner[input]{
return va
} else {
let firstPart = fibonacciCal(input-1, dictioner: &dictioner)
let secondPart = fibonacciCal(input-2, dictioner: &dictioner)
if dictioner[input] == nil {
dictioner[input] = firstPart+secondPart
}
return firstPart+secondPart
}
}
}
// 0,1,1,2,3,5,8
class TestRecursion {
func testRecursion () {
let t = Recursion()
t.fibonacci(3)
}
}