python - Determining time complexity for the following coded segment -
How do I set the complexity of time in this coded section? I'm new to Python and really appreciate any help!
sum = 0 i = n while (i> = 1): sum = = ii / = 2 i = nj = 2 while (i> = 1): sum + = ii / = Jj * = 2
I thought that the first loop can be (log n + 2) and another might be loop (2log n + 4) but I'm not sure I I'm close to living on the right track ...
Here my answer is to be more mathematically-accurate You should probably write inequality instead of the equation as I did, but the overall logic will not change anyway. I know from the comment if it makes sense or you get errors
The first loop
is to run multiple times as the first loop because it is less than 1.
The complexity of its time is O (log_2 (n + 1))
where log_2 based on log2 based on i mean 2.
0 i = N 1 i = n / 2 2 i = n / 2 2 ki = n / 2 ^ k
Ask yourself: What is Kashmir n = 2 ^ of
? ? Of course, you are not distracted by var yoga as there is no variable interference by running a loop.
Second loop
The second loop is also inspired by the division of I by a multi-2, but by multiplying it is speed on each moving j * 2
The complexity of its time is log_2 (n + 1) / 2.
1 i = n / 2 j = 2 2 i = n / 2/2/2 j = 4 3 i = n / 2/2/2/2/2/2 j = 8 k = n / 2 ^ (k) * 2 ^ (k) j = 2 ^ kki = n / 2 ^ (2k) j = 2 ^ k
< > K, j = 2 ^ k .
On the go k + 1
, j = 2 ^ (k + 1)
.
Then run k + 1
: i / = 2 ^ (k + 1)
Is it such that N = 2 ^ (2k)
?
2k log_2 (n) k = log_2 (n) / 2 log_2 (n) / 2 log_2 (sqrt (n))
code section
Two ends are not nested and I am resetting them between n, so they only do yoga.
log_2 (n + 1) + log_2 (sqrt (n + 1)) log_2 (n + 1 + sqrt (n + 1))
Comments
Post a Comment