ProgrammingVery hardCAT · MBA placement
Count the pairs (i, j) with 1 <= i <= j <= 10 for which the product i j is a perfect square. The procedure below does this with two nested loops. What is printed?
Count the pairs (i, j) with 1 <= i <= j <= 10 for which the product i * j is a perfect square. The procedure below does this with two nested loops. What is printed?
count = 0
for i = 1 to 10
for j = i to 10
if i*j is a perfect square then count = count + 1
end for
end for
Print count- A. 4
- B. 41
- C. 14
- D. 13
Correct answer: C. 14
The diagonal pairs i = j always qualify (10 of them), and the four pairs (1,4), (1,9), (2,8) and (4,9) add to 14. Requiring i < j gives 4, counting the non-squares gives 41, and shrinking the range to 9 gives 13.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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…
- 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…