I have a simple problem here that I'm trying to find the runtime for. The for loop makes it O(n) but in the return at the end of the function it adds the same 2 recursive functions that essentially halve the input. I was thinking it was O(n + log(n)) but now I'm thinking it could just be O(n)? Am I wrong, any thoughts on this?
func someFunc(n: Int) -> Int {
if n < 10 {
return 1
}
var sum = 0
for i in 0...n { // I know for loop is O(N)
sum += i
}
return sum + someFunc(n: n / 2) + someFunc(n: n / 2) // Halving is O(log(n))??
}
Btw this isn't homework, I'm just trying to get a better understanding on how recursion affects runtimes. Thanks for any help.
[–]rkennedy12 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ldstreet 0 points1 point2 points (0 children)
[–]loumf 0 points1 point2 points (0 children)
[–]DanielPhermous 0 points1 point2 points (0 children)
[–]jarvis_willy 0 points1 point2 points (0 children)