Control Flow & Loop Tracing
![Question 88, GATE 2003: In this C fragment j,k,n,TwoLog_n are integers, A is an integer array and n≥3. GFG defines TwoLog_n=2 floor(log2 n); GateForum instead uses 2 ceil(log2 n).
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);
What set is printed?
A. {m:m≤n and m=i! for some integer i}
B. {m:m≤n and m=i² for some integer i}
C. {m:m≤n and m is prime}
D. ∅](../../assets/questions/CS-2003-Q88-1.webp)
![Question 88, GATE 2003: In this C fragment j,k,n,TwoLog_n are integers, A is an integer array and n≥3. GFG defines TwoLog_n=2 floor(log2 n); GateForum instead uses 2 ceil(log2 n).
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);
What set is printed?
A. {m:m≤n and m=i! for some integer i}
B. {m:m≤n and m=i² for some integer i}
C. {m:m≤n and m is prime}
D. ∅](../../assets/questions/CS-2003-Q88-2.webp)
Answer & solution
Answer: D (no official key was published)
1. Logical OR leaves A[j]=0 only if every tested remainder j%k is zero. A nonzero remainder permanently sets the entry to 1. This is divisibility by every tested k, not primality testing.
2. Let L=TwoLog_n. For 3≤j≤L+1, the iteration k=j−1 is performed and leaves remainder 1, so none of those j prints.
3. For j>L+1, all k=2,...,L are tested. A printed j would therefore have to be a multiple of M=lcm(1,...,L). The useful bound M≥2L−1 follows because each binomial coefficient C(L−1,k) divides M/L, so summing the L coefficients gives 2L−1≤M.
4. With r=floor(log2 n) and L=2r, for n≥4 we have r≥2, hence M≥22r−1≥2r+1>n. No positive multiple can lie in the printing range. For n=3, k=2 marks 3 directly.
5. Nothing prints, D. The ceiling variant has at least the same tests, so also cannot print anything.
Solution: A[j] becomes true as soon as one tested k does not divide j; this is not a sieve that marks multiples. Let L be the outer-loop limit. If 3≤j≤L+1, the tested k=j−1 leaves remainder one, so j is not printed. For j>L+1, printing requires j to be divisible by every integer from 2 through L, hence by lcm(1,…,L). Let r=floor(log2 n), L=2r. For n≥4, the standard bound lcm(1,…,L)≥2L−1=22r−1≥2r+1>n rules this out. The n=3 case is directly marked by k=2. Thus nothing is printed, D. The LCM bound can also be established from the fact that every binomial coefficient in row L−1 divides lcm(1,…,L)/L, whose row sum is 2L−1.