This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fib (_ n: Int) -> Int { | |
guard n > 1 else {return n} | |
var a = 0 | |
var b = 1 | |
for _ in 2...n { | |
let temp = b | |
b = a + b | |
a = temp | |
} | |
return b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fib (_ n: Int) -> Int { | |
guard n > 1 else {return n} | |
return fib(n - 1) + fib(n - 2) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func countDown(_ num: Int){ | |
if (num < 1) {return} | |
print (num) | |
return countDown(num - 1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fact (_ num: Int) -> Int { | |
if num == 1 {return 1} | |
return num * fact(num - 1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fact (_ num : Int) -> Int { | |
if num == 1 {return 1} | |
var result = 1 | |
for i in 1...num { | |
result = result * i | |
} | |
return result | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func countDown(_ num: Int){ | |
if (num < 1) {return} | |
var number = num | |
while number > 0 { | |
print (number) | |
number -= 1 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cache : [Int] = [0] | |
func fib (_ num: Int) -> Int { | |
guard num > 1 else { return num } | |
if (cache.count > num) { | |
return cache[num] | |
} | |
let fibNum = fibRec(num - 1) + fibRec(num - 2) | |
cache.append(fibNum) | |
return fibNum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fib (_ num: Int) -> Int { | |
var first = [0, 1] | |
return fib(num, &first) | |
} | |
func fib(_ num: Int, _ cache: inout [Int]) -> Int { | |
if (cache.count > num) { | |
return cache[num] | |
} | |
let fibNum = fib(num - 1, &cache) + fib(num - 2, &cache) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fib(_ n: Int) -> Int { | |
guard n > 1 else { return n } | |
var cache : [Int] = Array(repeating: 0, count: n) | |
cache[0] = 0 | |
cache[1] = 1 | |
for i in 2..<n { | |
cache[i] = cache[i - 1] + cache[i - 2] | |
} | |
return cache[n - 1] + cache[n - 2] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fib (_ num: Int) -> Int { | |
guard num > 1 else { return num } | |
var a = 0 | |
var b = 1 | |
var c = 0 | |
for _ in 2..<num { | |
c = a + b | |
a = b | |
b = c | |
} |
OlderNewer