ProgrammingVery hardTCS NQT · Accenture
The procedure below uses the sieve of Eratosthenes on the range 2 to 50, marking composites starting from ii. The inner marking statement executes once for each multiple it marks. How many times is the marking statement executed in total?
The procedure below uses the sieve of Eratosthenes on the range 2 to 50, marking composites starting from i*i. The inner marking statement executes once for each multiple it marks. How many times is the marking statement executed in total?
for i = 2 to 50
if i is not marked then
for j = i*i to 50 step i
mark j; count = count + 1
end for
end if
end for
Print count- A. 45
- B. 62
- C. 77
- D. 49
Correct answer: A. 45
Marking starts at i*i, so the statement runs 45 times in total. Starting at 2i gives 62 runs, starting at i also marks each prime itself and gives 77, and counting every number instead of the primes gives 49.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Compute 3^13 mod 7 with the binary method below (a = 3, e = 13, m = 7). What is printed? result = 1; base = a mod m; e = 13 while e > 0 if…
- Let phi(n) be the number of integers i with 1 <= i <= n that share no common factor with n except 1. The procedure below computes phi(36)…
- Starting from n = 27, the Collatz step is applied: if n is even, n becomes n/2, otherwise n becomes 3n + 1. The procedure below counts the…
- Four numbers 4, 6, 10 and 15 are combined pairwise: the lcm of a and b is computed as ab div gcd(a, b), and the lcm of three numbers is…
- Count the integers n with 1 <= n <= 100 that are divisible by 3 or by 5, but NOT by both. The procedure below does this with a single…
- For N = 100 the procedure below counts how many times 5 divides the factorials, that is, the number of trailing zeros of 100!. The count…