Time complexity of nested while with changing condition











up vote
2
down vote

favorite












I'm trying to solve the complexity of this loop



for(int i= 0; i < n; i++) {    
c = i;
while(c > 1){
O(1);
c = c / 2;
}
}


as the while condition changes in every loop I don't know how to calculate that strange series.



I mean, if the loop where



for(int i= 0; i < n; i++) {     
c = n;
while(c > 1){
O(1);
c = c / 2;
}
}


I know the while has a complexity of O(logn) and it repeats itself n times, so the complexity would be O(nlogn).



The problem I have with previous loop is "c=i". As c=i, first time (c=0) the loop would reproduce 0 times, when c=1 it would reproduce 0 times again, when c=2 it would reproduce 1 time, then the series would follow and it is 0, 0, 1, 2, 2, 3, 3... (while reproductions each time of for loop)



O(logn) would not repeat itself n times, would repeat a number of times I can't come up with, so I don't know how to solve it.










share|improve this question


























    up vote
    2
    down vote

    favorite












    I'm trying to solve the complexity of this loop



    for(int i= 0; i < n; i++) {    
    c = i;
    while(c > 1){
    O(1);
    c = c / 2;
    }
    }


    as the while condition changes in every loop I don't know how to calculate that strange series.



    I mean, if the loop where



    for(int i= 0; i < n; i++) {     
    c = n;
    while(c > 1){
    O(1);
    c = c / 2;
    }
    }


    I know the while has a complexity of O(logn) and it repeats itself n times, so the complexity would be O(nlogn).



    The problem I have with previous loop is "c=i". As c=i, first time (c=0) the loop would reproduce 0 times, when c=1 it would reproduce 0 times again, when c=2 it would reproduce 1 time, then the series would follow and it is 0, 0, 1, 2, 2, 3, 3... (while reproductions each time of for loop)



    O(logn) would not repeat itself n times, would repeat a number of times I can't come up with, so I don't know how to solve it.










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm trying to solve the complexity of this loop



      for(int i= 0; i < n; i++) {    
      c = i;
      while(c > 1){
      O(1);
      c = c / 2;
      }
      }


      as the while condition changes in every loop I don't know how to calculate that strange series.



      I mean, if the loop where



      for(int i= 0; i < n; i++) {     
      c = n;
      while(c > 1){
      O(1);
      c = c / 2;
      }
      }


      I know the while has a complexity of O(logn) and it repeats itself n times, so the complexity would be O(nlogn).



      The problem I have with previous loop is "c=i". As c=i, first time (c=0) the loop would reproduce 0 times, when c=1 it would reproduce 0 times again, when c=2 it would reproduce 1 time, then the series would follow and it is 0, 0, 1, 2, 2, 3, 3... (while reproductions each time of for loop)



      O(logn) would not repeat itself n times, would repeat a number of times I can't come up with, so I don't know how to solve it.










      share|improve this question













      I'm trying to solve the complexity of this loop



      for(int i= 0; i < n; i++) {    
      c = i;
      while(c > 1){
      O(1);
      c = c / 2;
      }
      }


      as the while condition changes in every loop I don't know how to calculate that strange series.



      I mean, if the loop where



      for(int i= 0; i < n; i++) {     
      c = n;
      while(c > 1){
      O(1);
      c = c / 2;
      }
      }


      I know the while has a complexity of O(logn) and it repeats itself n times, so the complexity would be O(nlogn).



      The problem I have with previous loop is "c=i". As c=i, first time (c=0) the loop would reproduce 0 times, when c=1 it would reproduce 0 times again, when c=2 it would reproduce 1 time, then the series would follow and it is 0, 0, 1, 2, 2, 3, 3... (while reproductions each time of for loop)



      O(logn) would not repeat itself n times, would repeat a number of times I can't come up with, so I don't know how to solve it.







      algorithm time-complexity






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 12:13









      Dgrm

      386




      386
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          This need a bit of math involved.Given that log is well defined for a and b:



          log(a) + log(b) = log(ab)


          Here you have



          log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)


          There is a mathematical approximation for log(n!), namely



           log(n!) ~ nlog(n) - n + 1


          which reveal O(log(n!)= O(nlog(n))






          share|improve this answer





















          • why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
            – Dgrm
            Nov 7 at 18:33












          • @Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
            – UmNyobe
            Nov 8 at 10:21













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189257%2ftime-complexity-of-nested-while-with-changing-condition%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          This need a bit of math involved.Given that log is well defined for a and b:



          log(a) + log(b) = log(ab)


          Here you have



          log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)


          There is a mathematical approximation for log(n!), namely



           log(n!) ~ nlog(n) - n + 1


          which reveal O(log(n!)= O(nlog(n))






          share|improve this answer





















          • why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
            – Dgrm
            Nov 7 at 18:33












          • @Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
            – UmNyobe
            Nov 8 at 10:21

















          up vote
          4
          down vote



          accepted










          This need a bit of math involved.Given that log is well defined for a and b:



          log(a) + log(b) = log(ab)


          Here you have



          log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)


          There is a mathematical approximation for log(n!), namely



           log(n!) ~ nlog(n) - n + 1


          which reveal O(log(n!)= O(nlog(n))






          share|improve this answer





















          • why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
            – Dgrm
            Nov 7 at 18:33












          • @Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
            – UmNyobe
            Nov 8 at 10:21















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          This need a bit of math involved.Given that log is well defined for a and b:



          log(a) + log(b) = log(ab)


          Here you have



          log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)


          There is a mathematical approximation for log(n!), namely



           log(n!) ~ nlog(n) - n + 1


          which reveal O(log(n!)= O(nlog(n))






          share|improve this answer












          This need a bit of math involved.Given that log is well defined for a and b:



          log(a) + log(b) = log(ab)


          Here you have



          log(1) + log(2) +....+ log(n) = log(1*....*n) = log(n!)


          There is a mathematical approximation for log(n!), namely



           log(n!) ~ nlog(n) - n + 1


          which reveal O(log(n!)= O(nlog(n))







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 12:23









          UmNyobe

          17.5k73775




          17.5k73775












          • why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
            – Dgrm
            Nov 7 at 18:33












          • @Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
            – UmNyobe
            Nov 8 at 10:21




















          • why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
            – Dgrm
            Nov 7 at 18:33












          • @Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
            – UmNyobe
            Nov 8 at 10:21


















          why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
          – Dgrm
          Nov 7 at 18:33






          why the complexity is "log(1) + log(2) +....+ log(n)"? shouldn't it be "log(1) + log(2) +....+ log(n-1)" as 'for' condition is i<n?
          – Dgrm
          Nov 7 at 18:33














          @Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
          – UmNyobe
          Nov 8 at 10:21






          @Dgrm what's the difference. I can use the convention j = i+1. j goes from 1 to n while i goes from 0 to n-1
          – UmNyobe
          Nov 8 at 10:21




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189257%2ftime-complexity-of-nested-while-with-changing-condition%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud