Results 1 to 8 of 8
Like Tree5Thanks
  • 1 Post By chiro
  • 1 Post By zzephod
  • 1 Post By DenisB
  • 1 Post By DenisB
  • 1 Post By johng

Thread: determining number of iterations in a for loop

  1. #1
    Junior Member
    Joined
    Dec 2015
    From
    san fransisco
    Posts
    43
    Thanks
    1

    determining number of iterations in a for loop

    for i = 2 to n -1
    <do stuff>
    where 2<= i <= n-1

    to calculate the number of iterations we use the formula
    TOP INDEX - BOTTOM INDEX + 1
    so
    n-1 - 2 + 1 = n-3

    now take this loop

    for i = 2 to floor(n/2)
    <do more stuff>

    if n is even the number of iterations equals n/2
    if n is odd the number of iterations equals (n-1)/2

    Why is it that for the first example we add one after subtracting BOTTOM INDEX from TOP INDEX and in the second example we don't?


    Edit** realized i am in wrong section and this is probably more appropriate for discrete math, my apologies in advance.
    Last edited by mdm508; Dec 20th 2016 at 06:49 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Joined
    Sep 2012
    From
    Australia
    Posts
    6,572
    Thanks
    1710

    Re: determining number of iterations in a for loop

    Hey mdm508.

    I think you should write this out as a function of the iterations in the loop.

    In the first one you have 2 to n-1 and in the second you have 2 to floor(n/2).

    Use a common variable for the loop counter and then solve both of them in terms of that.
    Thanks from mdm508
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Senior Member zzephod's Avatar
    Joined
    Apr 2012
    From
    Erewhon
    Posts
    261
    Thanks
    153

    Re: determining number of iterations in a for loop

    Quote Originally Posted by mdm508 View Post
    for i = 2 to n -1
    <do stuff>
    where 2<= i <= n-1

    to calculate the number of iterations we use the formula
    TOP INDEX - BOTTOM INDEX + 1
    so
    n-1 - 2 + 1 = n-3

    now take this loop

    for i = 2 to floor(n/2)
    <do more stuff>

    if n is even the number of iterations equals n/2
    if n is odd the number of iterations equals (n-1)/2

    Why is it that for the first example we add one after subtracting BOTTOM INDEX from TOP INDEX and in the second example we don't?


    Edit** realized i am in wrong section and this is probably more appropriate for discrete math, my apologies in advance.
    In the second case suppose n=4 then floor(n/2) is 2 and the trip count is 1 (not n/2=2)
    again suppose n =5 then floor(n/2) is 2 and the trip count is 1 (not 4/2=2)

    .
    Thanks from mdm508
    Follow Math Help Forum on Facebook and Google+

  4. #4
    MHF Contributor
    Joined
    Feb 2015
    From
    Ottawa Ontario
    Posts
    1,654
    Thanks
    310

    Re: determining number of iterations in a for loop

    Quote Originally Posted by mdm508 View Post
    for i = 2 to floor(n/2)
    <do more stuff>
    if n is even the number of iterations equals n/2 ******
    Make it n=8 : floor(8/2) = 4

    for i = 2 to 4 : that's 3 iterations: 2,3,4

    BUT n/2 = 8/2 = 4

    ******????????????????????
    Thanks from mdm508
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Dec 2015
    From
    san fransisco
    Posts
    43
    Thanks
    1

    Re: determining number of iterations in a for loop

    Quote Originally Posted by chiro View Post
    Hey mdm508.

    I think you should write this out as a function of the iterations in the loop.

    In the first one you have 2 to n-1 and in the second you have 2 to floor(n/2).

    Use a common variable for the loop counter and then solve both of them in terms of that.
    See attachment below (PS is there anyway to do inline LaTex??)

    determining number of iterations in a for loop-codecogseqn-1-.gif
    my question is now is, why do we add one when we do TOPINDEX - BOTTOMINDEX + 1
    Last edited by mdm508; Dec 21st 2016 at 07:22 AM.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    MHF Contributor
    Joined
    Feb 2015
    From
    Ottawa Ontario
    Posts
    1,654
    Thanks
    310

    Re: determining number of iterations in a for loop

    Quote Originally Posted by mdm508 View Post
    my question is now is, why do we add one when we do TOPINDEX - BOTTOMINDEX + 1
    Illustration:
    use digits 2 to 7 to do "something"...

    So that's using 7 - 2 = 5 + 1 = 6 digits ; get my drift?
    Thanks from mdm508
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Dec 2015
    From
    san fransisco
    Posts
    43
    Thanks
    1

    Re: determining number of iterations in a for loop

    yes i think so, now suppose it was 2<=i < 7
    in this case 7-2 and boom we are done. so is it the greater or equal than sign that accounts for why we add 1?
    Follow Math Help Forum on Facebook and Google+

  8. #8
    MHF Contributor
    Joined
    Dec 2012
    From
    Athens, OH, USA
    Posts
    1,108
    Thanks
    461

    Re: determining number of iterations in a for loop

    Your last question is: given integers $a,\,b$ with $a\leq b$, how many integers $i$ are there with $a\leq i\leq b$?

    Take a specific example; $a=1,\,b=4$. The integers $i$ are $\{1,2,3,4\}\text{ or } 4\text{ possibilities}$, that is $b-a+1$.

    A related loop:

    Code:
    i=a;
    while (i<b) {
      // stuff
      i=i+1;
    }
    The above loop executes $b-a$ times! The difference is usage of $<$ instead of $\leq$. If you really want to do it, this can be proved by inducting on $n=a-b$. I think this is overkill. You should be able to believe it by examining small examples.
    Thanks from mdm508
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 0
    Last Post: May 28th 2016, 11:13 AM
  2. Number Loop
    Posted in the Advanced Statistics Forum
    Replies: 3
    Last Post: Oct 31st 2012, 05:59 PM
  3. determining abundant number?
    Posted in the Number Theory Forum
    Replies: 1
    Last Post: Aug 17th 2012, 02:02 AM
  4. Number of Iterations
    Posted in the Discrete Math Forum
    Replies: 6
    Last Post: Sep 16th 2010, 06:54 AM
  5. square number loop challange
    Posted in the Math Topics Forum
    Replies: 1
    Last Post: Oct 13th 2008, 04:39 PM

/mathhelpforum @mathhelpforum