ProgrammingHardTCS NQT · Accenture
The number of ways to write 20 as p + q with p and q prime and p <= q is counted by the procedure below, which tries p = 2 to 10 and sets q = 20 - p. What is printed?
The number of ways to write 20 as p + q with p and q prime and p <= q is counted by the procedure below, which tries p = 2 to 10 and sets q = 20 - p. What is printed?
count = 0
for p = 2 to 10
q = 20 - p
if p and q are both prime and p <= q then count = count + 1
end for
Print count- A. 4
- B. 8
- C. 5
- D. 2
Correct answer: D. 2
The pairs with p <= q summing to 20 are 3 + 17 and 7 + 13 (since 1 is not prime), so the count is 2. Letting p run to 20 counts the reversed pairs as well, counting all primes up to 20 gives 8, and using or instead of and counts many more pairs.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- For n = 1 to 100, the procedure below counts how many n satisfy that nn ends with the digit 1. What is printed? count = 0 for n = 1 to 100…
- The array a = [2, 3, 4, 1, 2, 3, 9] is scanned for its longest run of consecutive positions where each element is exactly one more than…
- The stack-based procedure below computes, for each index i of a = [4, 2, 6, 1, 5], the index of the next greater element to its right (or…
- An array a = [1, 3, 5, 7, 9, 11, 13, 15] is searched with binary search for the target 15, and the procedure counts how many array…
- The procedure below converts the Roman numeral "MCMXCIV" to a number by reading left to right and subtracting a symbol's value when the…
- A recursive function is written as below. The procedure counts the calls made for input 7, including the first call. What is printed?…