GATE Computer ScienceVery hardGATE CS (PYQ pattern)
for (i = 2; i <= 100; i++) if (!comp[i]) for (j = i * i; j <= 100; j += i) { comp[j] = 1; cnt++; } With comp[] initially all 0, what is the final value of cnt?
- A. 89
- B. 131
- C. 25
- D. 104
Correct answer: D. 104
Only primes i with i*i <= 100 (2, 3, 5, 7) enter the inner loop, which runs floor((100 - i*i)/i) + 1 times: 49 + 31 + 16 + 8 = 104 (note that composites are marked repeatedly, so cnt exceeds 75).
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- long p(long x, int n) { if (n == 0) return 1; if (n % 2 == 0) { long h = p(x, n / 2); return h h; } return x p(x, n - 1); } How many…
- void perm(char s, int k) { if (s[k] == 0) { puts(s); return; } for (int i = k; s[i]; i++) { char t = s[k]; s[k] = s[i]; s[i] = t; perm(s…
- void h(int n, char a, char b, char c) { if (n == 0) return; h(n - 1, a, c, b); printf("move disk %dn", n); h(n - 1, c, b, a); } During…
- Bubble sort with early exit (a pass that makes no swap ends the sort, and every pass scans the whole array from index 0 to n-2) is run on…
- char s[] = "GATECS"; for (int i = 1; s[i]; i++) s[i] = 'A' + (s[i] + s[i - 1] - 2 'A') % 26; printf("%s", s); What is printed?
- unsigned int i, s = 0; for (i = 5; i-- > 0; ) s += i; printf("%u %u", s, i); What is printed?