+ 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

10th May 2017, 2:12 PM
Nei Cardoso De Oliveira Neto
Nei Cardoso De Oliveira Neto - avatar
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.
10th May 2017, 2:49 PM
Rrestoring faith
Rrestoring faith - avatar
+ 6
I compared it in my computation speed comparer code. Can confirm it's close to O(n).
10th May 2017, 3:41 PM
Rrestoring faith
Rrestoring faith - avatar
+ 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.
10th May 2017, 2:22 PM
Nei Cardoso De Oliveira Neto
Nei Cardoso De Oliveira Neto - avatar
0
O(n^2)
10th May 2017, 2:19 PM
Thanh Le
Thanh Le - avatar
0
Ah... ok. Then it is (probably): O(log(i*j))
10th May 2017, 3:11 PM
Thanh Le
Thanh Le - avatar
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.
10th May 2017, 3:24 PM
Nei Cardoso De Oliveira Neto
Nei Cardoso De Oliveira Neto - avatar
0
shouldn't it depended on I and j, instead of n?
10th May 2017, 3:41 PM
Thanh Le
Thanh Le - avatar
0
No, because i and j are fixed; they're always incremented by their own value. Only n grows assimptocally.
10th May 2017, 3:48 PM
Nei Cardoso De Oliveira Neto
Nei Cardoso De Oliveira Neto - avatar
0
Rrestoring faith, good work.
10th May 2017, 3:48 PM
Nei Cardoso De Oliveira Neto
Nei Cardoso De Oliveira Neto - avatar