ITエンジニアが仕事に対して思うこと

ITエンジニアとして働く中で感じたことを、現場の温度感そのままに言語化するブログです。設計・実装・運用のリアル、学び続ける負荷、品質とスピードのせめぎ合い、コミュニケーションの難しさなど、きれいごとだけでは語れない「仕事の実態」を整理します。誰かを責めるのではなく、なぜそうなるのかを構造で捉え、明日から少し楽に、少し強く働ける視点を提供します。新人から中堅、マネジメントまで参考に。

【動画解説】ITPEC April 2025 FE (Fundamental IT Engineer) Subject B Q2| Step-by-Step Explanation

www.youtube.com In ITPEC April 2025 FE (Fundamental IT Engineer) Subject B Question 2, the program’s goal is to judge whether a given positive integer n is a perfect number. By definition, n is perfect when the sum of all its positive divisors excluding n itself (often called “proper divisors”) is exactly equal to n. This is a definition you can translate almost mechanically into code: enumerate candidates that might be proper divisors, select only those that divide n with no remainder, accumulate their values, and finally compare the accumulated sum with n. The blanks A and B exist precisely at the two critical points of that translation: deciding whether the current candidate k is a divisor, and deciding what to do with k when it is a divisor. Before filling the blanks, it is important to understand why the loop runs from k = 1 to half = ⌊n/2⌋. For any integer n ≥ 2, a proper divisor cannot exceed n/2. If k were greater than n/2 and still divided n, then n/k would be less than 2, which forces n/k to be 1 in integer terms, meaning k would have to be n itself. But n itself is excluded from the proper-divisor sum, so checking values larger than n/2 would add effort without ever adding a valid proper divisor. This is why computing half and looping only up to half is both correct and efficient. It covers every possible proper divisor while avoiding needless checks. Within that loop, A must be the condition that identifies “k is a divisor of n.” In integer arithmetic, the standard and exact way to express divisibility is to test whether the remainder of n divided by k is zero. Using the modulo operator, that condition is written as n mod k = 0. If n mod k is not zero, then k does not divide n and must not be included in the sum. Any other condition would either incorrectly include non-divisors (inflating the sum) or miss true divisors (deflating the sum), and either way the final comparison to n would no longer implement the perfect-number definition. Therefore, A must be n mod k = 0. Once A is true, the program has confirmed that k is a proper divisor, because k is within 1..⌊n/2⌋ and divides n evenly. At that moment, B must update the running total so that it becomes the sum of all proper divisors found so far. The essential detail is that the definition uses the sum of divisor values, not the count of divisors. That is why the update cannot be something like sum ← sum + 1, which would merely count how many divisors were found. Counting divisors answers a different question entirely (for example, it would make 6 look “close” to 3 rather than exactly equal to 6). To match the definition, the update must add the divisor itself: sum ← sum + k. This ensures that each qualifying divisor contributes its numeric value to the total, exactly as required. With these two blanks filled, the loop behaves in a way that is easy to justify formally. At the start, sum is 0, which equals the sum of proper divisors found in an empty search. Each iteration examines one candidate k. If k is not a divisor, sum remains unchanged, preserving the correctness of the running total. If k is a divisor, adding k updates sum so it equals the sum of all divisors discovered up to that point. When the loop ends, every possible proper divisor (every integer that could divide n without being n itself) has been considered, so sum equals the sum of all proper divisors of n. The final comparison “sum equals n” is therefore logically equivalent to the definition of a perfect number, and the function returns true exactly for perfect numbers. You can sanity-check the completed logic with simple examples. For n = 6, half = 3. The loop checks k = 1, 2, 3; each satisfies n mod k = 0, so sum becomes 1 + 2 + 3 = 6, and the function returns true. For n = 8, half = 4. The divisors in that range are 1, 2, and 4, so sum becomes 7, which is not 8, and the function returns false. For n = 28, half = 14. The divisors are 1, 2, 4, 7, and 14; their sum is 28, so the function returns true. These outcomes match the mathematical classification of perfect numbers and confirm that the blanks were filled in a way that preserves the intended behavior. In short, the only completion consistent with the program’s stated objective is A = n mod k = 0 and B = sum ← sum + k. This combination corresponds to choice d in the answer group, and it is also the only choice that makes the loop compute the correct quantity: the sum of proper divisors used to decide whether n is perfect.