ProgrammingHardTCS NQT · Accenture
Count the numbers n with 1 <= n <= 100 such that both n and n + 2 are prime (twin primes, lower member n). The procedure below checks primality by trial division and prints the count. What is printed?
Count the numbers n with 1 <= n <= 100 such that both n and n + 2 are prime (twin primes, lower member n). The procedure below checks primality by trial division and prints the count. What is printed?
count = 0
for n = 1 to 100
if n is prime and n + 2 is prime then count = count + 1
end for
Print count- A. 1
- B. 8
- C. 25
- D. 50
Correct answer: B. 8
The lower members are 3, 5, 11, 17, 29, 41, 59 and 71, which is 8. Checking n and n + 1 gives only 2 and 3 which is 1, counting the primes up to 100 gives 25, and counting odd pairs without a primality test gives 50.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below counts the odd divisors of 1000 by testing every d from 1 to 1000. What is printed? count = 0 for d = 1 to 1000 if…
- Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn = Fn-1 + Fn-2. The procedure below computes F50 by iteration and then prints F50…
- For a = 240 and b = 46, the extended Euclid procedure below keeps x and y so that ax + by equals the gcd. What is the value of x printed…
- Array a = [-2, 1, -3, 4, -1, 2, 1, -5, 4]. Kadane's algorithm below finds the maximum sum of a non-empty contiguous subarray. What is…
- Array a = [3, 4, 1, 2, 4, 2, 1, 3] and target K = 7. The procedure below counts contiguous subarrays whose sum is exactly K, using prefix…
- Insertion sort on the array [5, 2, 4, 6, 1, 3] shifts an element one place right each time it moves past a larger element. The procedure…