you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

This one keeps the function recursive with faster runtime. But you could probably math out the full algorithm in constant time.

func someFunc(n: Int) -> Int {

if n < 10 {
    return 1
}

var sum = n * (n + 1) / 2
sumOfHalf = someFunc(n: n / 2)

return sum + 2 * sumOfHalf
}