Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2011-05-02 00:37:40

mantastrike
Member
Registered: 2011-04-24
Posts: 64

How many times will the innermost loop...HELP ME!!

How many times will the innermost loop of the following algorithm be run? Assume n and n and k are positive integers.

Please help me with this with a detailed answer. You don't have to explain but please show some working.

for i : = 1 to 17
            for j: = 1 to n
                  for k: = 1 to m
                              [Statements in body]
                    next k
              next j
next i

Please help me guys! THANK YOU!!


I love this forum for being a great help!

Offline

#2 2011-05-02 05:13:40

gAr
Member
Registered: 2011-01-09
Posts: 3,479

Re: How many times will the innermost loop...HELP ME!!


"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense"  - Buddha?

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

Offline

#3 2011-05-04 16:54:20

mantastrike
Member
Registered: 2011-04-24
Posts: 64

Re: How many times will the innermost loop...HELP ME!!

gAr wrote:

can u plz tell me how you got this? can u plz show work?

Offline

#4 2011-05-04 17:02:37

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 86,309

Re: How many times will the innermost loop...HELP ME!!

Hi mantastrike;

The innermost k loop goes from 1 to m. If a loop goes from 1 to 5, that is 5 times. If it goes from 1 to 100, that is 100 times. If it goes from 1 to m that is m times.

Now the j loop goes from 1 to n. That is n times. Each time the j loop counts once the k loop goes m times.

So the two inner loops are n*m

The i loop goes from 1 to 17. Each time the i loop executes the j and  k loop execute n*m

So all three loop s are 17 * n * m


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#5 2011-05-29 19:09:19

umasou
Member
Registered: 2011-05-29
Posts: 2

Re: How many times will the innermost loop...HELP ME!!

I have the same problem but different different values [assume a,b,c,d are all +ve]:

assume a ≤ b and c ≤ d

for j= a to b
   for h = c to d
        [Statements in body]
   next h
next j

yikes hmm..should there be more than 1 answer..?

Offline

#6 2011-05-29 19:13:31

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 86,309

Re: How many times will the innermost loop...HELP ME!!

Hi umasou;

Welcome to the forum! There is still a unique answer. The body of the loops will executed ( b - a + 1 ) ( d - c + 1 ) times.

This is assuming an increment of 1.


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#7 2011-05-29 19:23:57

umasou
Member
Registered: 2011-05-29
Posts: 2

Re: How many times will the innermost loop...HELP ME!!

hiya. thanks for welcoming me big_smile, this place is cool.

I'm not clear with, if a=b, c=d, what will happen to the loop?

thanks again

Offline

#8 2011-05-29 19:28:23

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 86,309

Re: How many times will the innermost loop...HELP ME!!

If a = b then it will execute 1 time.

Use the formula, if a = b and c =d then the body will be executed:

( b - a + 1 ) ( d - c + 1 ) = ( 0 + 1) ( 0 + 1 ) = 1 time

And wait, I forgot to mention this. Why are you asking this question? All you have to do to answer this for yourself is program two loops and put a print statement between them. You are both missing the whole point of programming. It is to answer experimentally, questions like this.


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

Board footer

Powered by FluxBB