The Gateway to Computer Science Excellence
+3 votes
79 views

In the code fragment on the right, start and end are integer values and $\text{prime}(x)$ is a function that returns true if $x$ is a prime number and $\text{false}$ otherwise. At the end of the loop:

i := 0; j := 0; k := 0;
for (m := start; m <= end; m := m+1){
k := k + m;
if (prime(m)){
i := i + m;
}else{
j := j + m;
}
}
  1. $k < i+j$
  2. $k = i+j$
  3. $k > i+j$
  4. Depends on $\text{start}$ and $\text{end}$
in Algorithms by Veteran (105k points)
edited by | 79 views

2 Answers

+1 vote

Answer: B

Explanation:

i := 0; j := 0; k := 0;
for (m := start; m <= end; m := m+1){
k := k + m; // "k" will have the accumulated sum from start to end.
if (prime(m)){
i := i + m; //prime values are added to "m".
}else{
j := j + m; //Values which are NOT prime are added to "j".
}
}

Here,

$\large \color {red}{{\mathrm {\large k}}}$ will contain the sum of all the values from start to end.

$\color {red} {\mathrm {\large i}}$ will contain the sum of values which are prime.

$\color {red} {\mathrm {\large j}}$ will contain the sum of values which are NOT prime.

Hence, finally the values in k is the sum of values in i and j. 

$\therefore$ The relation is: $\color {blue}{\mathrm {\large k = i + j} }$

Hence, is the right option.

 

by Boss (18.9k points)
edited by
+1
Nicely explained. 👍
+1
Thanks.
0 votes

B. $k = i + j$

Whatever be the value of m, the value added to k is also added to exactly one of i and j.

by Boss (36.5k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,279 answers
198,173 comments
104,840 users