简体   繁体   中英

How many times is this loop body repeated?

In my Object-Oriented Programming course we discussed a topic that I don't think he ever named, I've tried to find out what it's name is to find a proper way to solve these, but I have had no luck.

This is not homework, but a question for clarification about the process to solve this problem.

for I = (N + 2) downto -1
    for J = (I - 1) to (N + 4)
        // Code is run here

The question is "How many times is // Code is run here ran?"

Here is what I have tried to solve this:

1) I = (N + 2) , J = [(N + 2) - 1] from this (and what I remember) you use b - a - 1 to solve for the number of times executed, which gives us X = [(N + 2) - 1] - (N + 2) - 1 which can be simplified to X = -2

2) I = -1 , J = ((-1) - 1) and X = ((-1) - 1) - (-1) - 1 which simplifies to X = -2`

I'm getting lost on dealing with the second for loop and how to finish the problem. I know that we have to end up with an answer such as r(r + 1)/2

I just want to say that I have attempted to look for a name of this type of technique, but he simply called it "Code Counting" which didn't return any searches relating to this topic.

Thank you

EDIT: This course was in Java, so that is why I used the Java tag for this question, if anyone is curious.

EDIT2: To clarify, this was on a written exam , so we are expected to do this via pen-and-paper, I would like an explanation of how to solve this question as I have attempted it many times and still end up with the wrong answer.

Just look at the "code" and start counting logically. In the first iteration of the outer loop (called OL) you execute the inner loop (IL) (N + 4) - (N + 2 - 1) + 1 times = 4 times.

Explanation of the +1 : if we run the loop from -1 to 2, we in fact run it 4 times: -1, 0, 1, 2, which in math is `2 - (-1) + 1.

The next time I = N + 1 , therefore the IL runs (N + 4) - (N + 1 - 1) + 1 times = 5 times. Same goes for the next step and the step after that, the times the IL is executed increase by one each time : 4 + 5 + 6 + ... . The question remaining is how far we go.

The last step is I = -1 , there IL gets run (N + 4) - (-1 - 1) + 1 = N + 7 times.

The sum you are looking for therefore seems to be 4 + 5 + 6 + ... + (N + 6) + (N + 7) . Which in fact is something like r(r + 1)/2 with a few substractions and additions.

The above numbers assume the to boundaries to be inclusive.

Note: whenever you come up with some kind of a formular, choose the input parameter as something small (like 0 or 1) and verify that the formula works for that value.

Summing the values using the little gaussian formula r * (r + 1) / 2 we have r -> N + 7 . And therefore (N + 7) * (N + 8) / 2 . But then we count the 3, 2 and 1 as well, which are actually not in the above listing, we need to subtract them and come to the final solution of:

(N + 7) * (N + 8) / 2 - 6

The algorithm as shown in the question looks like the good old Basic syntax

for X down/to Y, that includes Y

The outer loop goes from n+2 to -1 , so the inner loop goes

n+1 to n+4 =>   4 iterations
...
 -2 to n+4 => n+7 iterations

Summing all of these, we get

n+3
 ∑ (4+i)  =  4(n+4) + (n+3)(n+4) / 2
i=0
          =  (n+11)(n+4) / 2

which is also equal to (N + 7)(N + 8) / 2 - 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM