ProgrammingHardTCS NQT · Accenture
The procedure below finds the largest prime less than 100 by testing each candidate from 99 down to 2 for divisibility by 2 up to its square root. What is printed?
The procedure below finds the largest prime less than 100 by testing each candidate from 99 down to 2 for divisibility by 2 up to its square root. What is printed?
for n = 99 down to 2
prime = true
for d = 2 to sqrt(n): if n mod d == 0 then prime = false
if prime then print n and stop
end for- A. 89
- B. 99
- C. 101
- D. 97
Correct answer: D. 97
97 is prime and nothing between 98 and 99 is, so the answer is 97. Limiting the search below 91 gives 89, dropping the primality test gives 99, and extending the range to 101 gives 101, which is not below 100.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below sums the integers from 1 to 100 that are NOT divisible by 2 and NOT divisible by 3. What is printed? total = 0 for n =…
- 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…
- 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…