search
Log In
3 votes
144 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
edited by
144 views

2 Answers

2 votes

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.

 


edited by
1
Nicely explained. πŸ‘
1
Thanks.
0
I think the below line:-

i := i + m; //prime values are added to "m".

should be changed to:-

i := i + m; //prime values are added to "i".
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.

Related questions

2 votes
1 answer
1
98 views
Consider the code below, defining the function $A$: A(m, n, p) { if (p == 0) return m+n; else if (n == 0 && p == 1) return 0; else if (n == 0 && p == 2) return 1; else if (n == 0) return m; else return A(m, A(m,n-1,p), p-1); } Compute $A(2, 2, 3)$ and $A(2, 3, 3)$.
asked May 27, 2016 in Algorithms jothee 98 views
2 votes
1 answer
2
229 views
Consider the code below, defining the function $A$: A(m, n, p) { if (p == 0) return m+n; else if (n == 0 && p == 1) return 0; else if (n == 0 && p == 2) return 1; else if (n == 0) return m; else return A(m, A(m,n-1,p), p-1); } Express $A(m, n, 2)$ as a function of $m$ and $n$.
asked May 27, 2016 in Algorithms jothee 229 views
3 votes
1 answer
3
109 views
Consider the code below, defining the function $A$: A(m, n, p) { if (p == 0) return m+n; else if (n == 0 && p == 1) return 0; else if (n == 0 && p == 2) return 1; else if (n == 0) return m; else return A(m, A(m,n-1,p), p-1); } Express $A(m, n, 1)$ as a function of $m$ and $n$.
asked May 27, 2016 in Algorithms jothee 109 views
1 vote
0 answers
4
82 views
$\text{Air CMI }$operates direct flights between different cities. For any pair of cities $A$ and $B$, there is at most one flight in a day from $A$ to $B$. The online site $FakeTrip$ ... to list all pairs of cities connected by a route on which all connections are feasible within the same day. Analyze the complexity of your algorithm.
asked May 27, 2016 in Algorithms jothee 82 views
...