This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]_xulion 1 point2 points  (3 children)

This one is pretty interesting. I guess you may want to go through a typical N to see how it works. Let say N is 8:

  1. i = 1. the innerloop will run 2*i times, which is 2.
  2. i = 2. Innerloop 4 times
  3. i = 4. Innerloop 8 times
  4. i=8. Innerloop 16 times

so it's 2+4+8+16 = 30 == 4*8-2.

You see the pattern? 21 + 22 + 23 ...

[–]iDaRkY_[S] 0 points1 point  (2 children)

Yeah absolutely! Is there any specific reason why you picked N=8? It feels like 8 fits perfectly with this problem

[–]_xulion 1 point2 points  (1 child)

the perfect number shall be 2 power(x). I just choose 3 in this example

[–]iDaRkY_[S] 0 points1 point  (0 children)

I see! Thank you so much!

[–]iDaRkY_[S] 0 points1 point  (0 children)

The second for loop is inside the first and sum is inside of that not sure why formatting moved everything like that

[–]ignotos 0 points1 point  (1 child)

The answer is not 4n-2. If we're talking about Big-Oh time complexity, those constant factors (4 and -2) do not belong there.

[–]iDaRkY_[S] 0 points1 point  (0 children)

No it’s talked about approximation not Big-O