ITPEC FE Subject B October 2024 Question 4
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
The procedure primeFactors outputs the prime factors for the input values as its argument. The first few prime numbers are 2, 3, 5, 7, 11, and 13. For instance, if the input integer is 12, the output is “2×2×3”. If the input integer is 78, the output is “2×3×13”. The input integer must be greater than 1.
Answer group
Explanation
A: num mod i = 0, B: num > 1 — divide out i while it divides num, move to the next i when it does not, and stop once num is down to 1.
Trace for 12:
The while test 1 > 1 is false, so the loop ends with 2×2×3. Blank B serves twice, because both questions are the same: is anything left to factor? If so, another factor follows (print ×) and the loop continues. Each i that divides num is prime, because all smaller factors have already been divided out.
Answer (c)
Why not others:
- •(a) —
num < 1is never true for a positivenum, so nothing is divided out;ibecomes 3, thewhiletest12 mod 3 ≠ 0is false, and the output is empty - •(b) —
num > 1divides without checking divisibility: 12 becomes 6 and 3 while 2 is output twice, then3 mod 2 = 0is false and the loop stops at "2×2", losing the factor 3 - •(d) — the divisibility test is inverted: 12 is divisible by 2, so
imoves on to 3, and thewhiletestnum < 1is false at once, so nothing is output
Key rule: In trial division keep dividing by the same i while num mod i = 0 before moving on, and loop while the remaining quotient is greater than 1.
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.