# Thread: calculating the results of loops

1. ## calculating the results of loops

Hi....
I am studying in 10th class and also studying for Computer Olimpiad Games. In first exam, there will be some loop problems which i can solve in some mins but i want to make my speed faster to solve them. So i am asking your ways to solve those loops. p.s:"not the easy ones. As in the example. I can send you more types if you ask for..."

----------------------
example:
---------------------------------
int a,b,c,m=0;
for(a=1;a<=62;a++)
for(b=a;b<=62-a;b++)
for(c=b;c<=31;c++)
m+=a+b+c;
----------------------------------
what is the last value of m?
----------------------

2. The code might be easier to understand if you format it properly:
Code:
```int a,b,c,m=0;
for(a=1;a<=62;a++)
for(b=a;b<=62-a;b++)
for(c=b;c<=31;c++)
m=a+b+c;```
You might try running that code through a debugger.

3. sorry for not writing my code as you did. I am new at forum and didnt know that if i can do this before i sent my message. By the way, I am asking the calculating way with maths, not compiler. Because in exam, they give us some codes like that and asks:"how many time does the '....' code loop?" or "what is the final value of '...' ".

Code:
```n=5;
for(a=1;a<=n;a++,m+=a)
for(b=1;b<=n;b++,m+=b)
for(c=1;c<=n;c++,m+=c)```
for such a code, you can easily formulise the result as:
[(n.(n+1)/2).(n.n+n+1)]

but in complicated loops, there are more ways to do and to calculate. I am asking to everyone to find the best way at last to use. Let's begin with the loop which i wrote at the beginning of thread.

4. I don't think I fully understand your question....but if you just look at the loops logically they'll be pretty easy to figure out. For example the one you showed m would equal 31+31+31 for its final value because if the value of c is more than 31 it will never do that final loop, and that means the highest values a and b can be for their loops in order to make the final loop trip is 31. So m's final value would be 93.

5. the question is that: "How can you formulise loops?" "What ways do you try?"
For example:

Code:
```n=10;
m=0;
for(a=1;a<=n;a++)
for(b=1;b<=a;b++)
m++;```
for this code i do such a thing:
--------------------------------
a I b I m
1 I 1 I 1
2 I 1 I 2
I 2 I 3
3 I 1 I 4
I 2 I 5
I 3 I 6
--------------------------------

As i was doing such a table, usually something warns me and i can formulise the loops easily. For this quesiton it is [n.(n+1)/2]

I am asking that if i could formulise every loop or not? And if i can, is there any easy ways to do or is there any resources helps me about this?

Also asking; if you were me, what could you do to formulise them? What would your way be?

6. oh i think nobody want to discuss about it or have idea

7. You're not supposed to bump threads.

That is a pretty tough looping construct. Try searching the board.