ITPEC FE Subject B April 2025 Question 2
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
The function isPerfect receives positive number n, and returns whether n is a perfect number. Here, a number is a “perfect number” if the sum of its positive divisors (excluding the number itself) is equal to the number itself. For instance, 28 is a perfect number because 28 has divisors 1, 2, 4, 7, and 14, and 1 + 2 + 4 + 7 + 14 = 28.
Answer group
Explanation
A: n mod k = 0, B: sum ← sum + k — pick out each k that divides n and add k itself, then compare the total with n.
A proper divisor of n is never larger than n ÷ 2, which is why the loop stops at half. Inside the loop the program must keep only the divisors (n mod k = 0) and add the divisor's value to sum, not a count.
Trace with n = 28, half = 14: the test passes at k = 1, 2, 4, 7 and 14, so sum runs 1, 3, 7, 14, 28. Since 1 + 2 + 4 + 7 + 14 = 28, sum = n and the function returns true. For n = 12 the divisors 1, 2, 3, 4, 6 give 16, so it returns false.
Answer (d)
Why not others:
- •(a) — adds 1 for every k that does not divide n, so it counts non-divisors: 9 for 28 (3, 5, 6, 8, 9, 10, 11, 12, 13), and 28 is rejected.
- •(b) — sums the non-divisors instead: 3 + 5 + 6 + 8 + 9 + 10 + 11 + 12 + 13 = 77 for 28.
- •(c) — the right test, but adding 1 makes
sumthe number of proper divisors, 5 for 28, not their total.
Key rule: A counter adds 1 per hit, an accumulator adds the value itself; check which one the specification asks for.
Math & Numbers · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.