How many times does this cycle body repeat?

In my course on object-oriented programming, we discussed a topic that I don’t think he ever called, I tried to find out what it was to find the right way to solve them, but I was out of luck.

This is not homework, but a question for clarifying the process of solving this problem.

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

Question: "How many times // Code is run here works?"

Here is what I was trying to solve:

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 get lost when working with the second for loop and how to end the problem. I know that we should get an answer, for example r(r + 1)/2

I just want to say that I tried to find the name of this type of technique, but he just called it "Code Counting", which did not return any queries related to this topic.

thanks

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

EDIT2: To clarify, this was a written exam , so we had to do it with pens and paper, I would like to explain how to solve this question, since I have tried many times to do it and still got the wrong answer.

+5
source share
2 answers

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 +1: if we run a cycle from -1 to 2, we actually execute it 4 times: -1, 0, 1, 2, which in mathematics is 2 (-1) + 1.

Next time I = N + 1 , so IL starts (N + 4) - (N + 1 - 1) + 1 times = 5 times. The same applies to the next step and the step after that, the execution time of the IL increases by one each time: 4 + 5 + 6 + ... The question remains, how far are we going.

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

So the amount you are looking for seems 4 + 5 + 6 + ... + (N + 6) + (N + 7) . Actually it is something like r(r + 1)/2 with several additions and additions.

In the above numbers, it is assumed that to borders should be included.

Note: whenever you come up with some form, select the input parameter as something small (e.g. 0 or 1) and make sure that this formula works for this value.

Summing up the values ​​using the small Gaussian formula r * (r + 1) / 2 , we have r -> N + 7 . And so (N + 7) * (N + 8) / 2 . But then we also count 3, 2 and 1, which are not actually indicated in the list above, we need to subtract them and come to a final decision:

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

The algorithm, as shown in the question, looks like the old old 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 up all this, 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

+1
source

Source: https://habr.com/ru/post/1239177/


All Articles