• recategorized by
1,685 views
4 4 votes

In the following C program fragment, j, k, n and TwoLog_n are integer variables, and A is an array of integers. The variable n is initialized to an integer ≥ 3, and TwoLog_n is initialized to the value of $2^*\lceil \log_2(n) \rceil$

for (k = 3;  k <= n; k++)
        A[k] = 0;
for (k = 2; k <= TwoLog_n; k++)
    for (j = k+1; j <= n; j++)
        A[j] = A[j] || (j%k);
for (j = 3; j <= n; j++)
    if (!A[j]) printf("%d", j);

Can someone go step by step and say me what is happening here?

[All a[j] will get !=0 value here,rt?]

Plz explain

2 Answers

1 1 vote
A[j] = 0, if j % k == 0, for j >= 3 && j <= N and k >= 2 and k <= $2*\log_2N$.

Else A[j] = 1. Once some A[j] becomes 1 it will remain that way through out the execution.

This means that it checks for all j in the range 3 to N, wether that j is divisible by all k from 2 to $2*\log_2N$.

For indices satisfying above criteria, A[j] will be 0 an for the rest it will be 1.

The printing loop at the end will print those indices where A is 0.

 

Finally the loop will not print anything. Check comments below.
• edited by
1 1 vote

Say n=6

Now, in at last loop we are printing j=3 to 6

which all are non zero.

We will print those j which are !A[j]=1

means A[j]=0

But in j=3 to 6 there is no such value.

So, print nothing

Position:
Show:

Related questions

5 5 votes
3 answers 3 answers
640
640 views
Vyshakh_Nambiar asked Dec 8, 2025
640 views
Should this question be invalid? t[4] Mentions 4 elements, but question assigns 5 elements.Because according to my knowledge accessing element t[4] (if we consider index ...