+ 2
What's the time complexity (big-O) of two nested for loops with the same length that increment doubling the iterator variable?
Like this: for i=1; i to n; i=i+i: for j=1; j to n; j=j+j: //do stuff
9 ответов
+ 6
I believe it would be closer to O(n(^1/2*2))
= O(n)
Square root because of how the itteration works. Times 2 because there's another loop. Could be wrong though.
Perhaps compare the computation speed with an O(n) loop, and see how close they were.
+ 6
I compared it in my computation speed comparer code. Can confirm it's close to O(n).
+ 1
Actually it cannot be O(n**2). It has to be much smaller as the variables only iterate over powers of two and powers of two get exponentially scarcer as n grows.
0
O(n^2)
0
Ah... ok. Then it is (probably):
O(log(i*j))
0
I think we need a formula that returns the number of powers below a given threshold n. Then we could multiply that by itself.
Whether that's log or sqrt I don't have the know-how to determine.
0
shouldn't it depended on I and j, instead of n?
0
No, because i and j are fixed; they're always incremented by their own value. Only n grows assimptocally.
0
Rrestoring faith, good work.