• edited by
53,411 views
111 111 votes

Consider the following pseudo code. What is the total number of multiplications to be performed?

D = 2    
for i = 1 to n do
    for j = i to n do
        for k = j + 1 to n do
            D = D * 3 
  1. Half of the product of the $3$ consecutive integers.
  2. One-third of the product of the $3$ consecutive integers.
  3. One-sixth of the product of the $3$ consecutive integers.
  4. None of the above.

15 Answers

Best answer
143 143 votes

Total number of multiplications

$= \displaystyle \sum_{i=1}^{n}\sum_{j=i}^{n}\sum_{k=j+1}^{n} 1$
$= \displaystyle\sum_{i=1}^{n}\sum_{j=i}^{n}(n-j)$
$=\displaystyle \sum_{i=1}^{n}(n-i) + (n-(i+1)) + \ldots+ (n-n)$
$\displaystyle =\frac{1}{2}\sum_{i=1}^{n}(n-i)(n-i+1)$
$\displaystyle =\frac{1}{2}\sum_{i=1}^{n}(n^{2} +n+i^{2}-(2n+1)i)$
$\displaystyle = \frac{1}{2} \left( n^3 + n^2 + \sum_{i=1}^{n}i^2 - (2n+1). \sum_{i=1}^{n} i \right)$
$\displaystyle=    \frac{1}{2} \left( n^3 + n^2 + \frac{n. (n+1). (2n+1)}{6} - (2n+1). \frac{n. (n+1)}{2} \right)$
$ \displaystyle = \frac{1}{2} \left( n^3 + n^2  - \frac{n. (n+1). (2n+1)}{3} \right)$
$\displaystyle =  \frac{n}{6} \left( 3n(n+1)   -  (n+1). (2n+1) \right)$
$ \displaystyle= \frac{n. (n+1)}{6} (n-1)$
$ \displaystyle =\frac{(n-1)n(n+1)}{6}$

Therefore, correct answer would be (C).

• edited by
56 56 votes

 

Same as Best answer, but I Cleared some Basic steps more.

• edited by
31 31 votes

i goes 1 to n

j goes i to n

k goes j+1 to n


let's take first iteration for i: as i=1

j goes 1 to n

for j=1 , k: 2 to n = (n-1) times
    j=2 , k: 3 to n = (n-2) times
    ...
  j=n-1 , k: n to n = 1 time
  j= n  , k: 0 times


so, for i=1,
 number of multiplications : 1 + 2 + ... + (n-1)

similarly for i = 2,
 1+2+ .... + (n-2)

and so on,

for i = (n-1)
   1

for i = n
  0

so, for every i say last term as T

we have for every i, 1+2+3+... + T = T(T+1)/2 = (T^2  + T)/2

where T goes from 1 to (n-1)

∑  (T^2  + T)/2 , T goes 1 to (n-1) = (n-1)(n)(n+1)/ 6

so, option C is correct

21 21 votes
C

Take a simple example with n=3

It will give 4multiplications. For verification take other example with n=4.

Only choice which satisfy is c
14 14 votes
i j Inner Loop Iternations
1 1 n-1
1 2 n-2
1 .. ..
1 .. ..
1 n 0
i j Inner Loop Iterations
2 2 n-2
2 3 n-3
2 .. ..
2 .. ..
2 n 0
i j Inner Loop Iterations
n-1 n-1 1
n-1 n 0
i j Inner Loop Iternations
n n 0

Total Number of Inner Loop iterations = (0) + (0+1) + (0+1+2) + (0+1+2+3) +............+ (0+1+2+3+....n-1)

= ∑i=0 to n-1   (0+1+2+.....i)  

= ∑i=0 to n-1   (i*(i+1))/2

= ∑i=0 to n-1   (i2 + i)/2

= [ (n-1)(n)(2n-1)/6 + (n-1)n/2 ] / 2

= (n-1)n(n+1)/6

Hence C option is the correct answer. 

6 6 votes

Option C

When difference of the terms of the series form an A.P., then how to write any random term for that series ?

Source :  https://math.stackexchange.com/questions/1961952/how-do-i-find-the-sum-of-a-sequence-whose-common-difference-is-in-arithmetic-pro

Note : There are n-1 terms in the series, I took n terms for easier calculation. It won't affect the answer at all. 

 

 

 

Answer:
Position:
Show:

Related questions

80 80 votes
11 answers 11 answers
34.2k
34.2k views
go_editor asked Sep 28, 2014
34,182 views
Consider a $6$-stage instruction pipeline, where all stages are perfectly balanced. Assume that there is no cycle-time overhead of pipelining. When an application is exec...
62 62 votes
6 answers 6 answers
22.5k
22.5k views
go_editor asked Sep 28, 2014
22,517 views
Consider the following C function in which size is the number of elements in the array E: int MyX(int *E, unsigned int size) { int Y = 0; int Z; int i, j, k; for(i = 0; i...
138 138 votes
19 answers 19 answers
81.8k
81.8k views
go_editor asked Sep 28, 2014
81,835 views
The minimum number of comparisons required to find the minimum and the maximum of $100$ numbers is ________
71 71 votes
13 answers 13 answers
19.1k
19.1k views
go_editor asked Sep 28, 2014
19,052 views
There are $5$ bags labeled $1$ to $5$. All the coins in a given bag have the same weight. Some bags have coins of weight $10$ gm, others have coins of weight $11$ gm. I p...