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

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

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

      [https://www.youtube.com/watch?v=zIEXhLZjqLE:embed:cite]

The program computes the greatest common divisor using the subtraction-based Euclidean algorithm. When GCD(98, 56) is called, it assigns the input values to two working variables, m and n, and then repeatedly reduces the larger one by subtracting the smaller one. The loop continues while m and n are different, and the point that matters most for this question is the placement of the output statement: the program prints the pair (m, n) immediately after it updates either m or n, before it returns to the top of the loop to check whether m != n is still true. That is why the printed sequence includes every intermediate state produced by each subtraction step, including the final state where the two values become equal. At the start, initialization sets m = 98 and n = 56. The loop condition asks whether m and n are unequal. Because 98 and 56 are not equal, the loop body executes. Inside the loop, the program compares the two values. Since m > n is true at this moment, it updates m by subtracting n from it. The new value becomes m = 98 − 56 = 42, while n remains 56. Immediately after this update, the program outputs the current values, so the first printed pair is 42 56. Only after printing does control flow return to the loop header. The loop condition is evaluated again. Now m = 42 and n = 56, so they are still different and the loop continues. The comparison m > n is checked again, but this time it is false because 42 is not greater than 56. Therefore, the program takes the else-branch and updates n instead, subtracting m from n. The new value becomes n = 56 − 42 = 14, while m stays 42. Right after updating n, the program outputs the pair, producing the second printed values 42 14. Once again, the condition is not checked until the next iteration begins. The third iteration starts with m = 42 and n = 14. Since they are not equal, the loop runs. Now m > n is true, so the program updates m by subtracting n: m = 42 − 14 = 28. The variable n remains 14. The program then outputs the current pair, so the third printed pair is 28 14. This reflects exactly what the algorithm is doing: it is shrinking the larger number while preserving the GCD of the pair. In the fourth iteration, m = 28 and n = 14 are still not equal, so the loop continues. The comparison m > n is again true, and m is updated as m = 28 − 14 = 14. Now both variables are equal, with m = 14 and n = 14. However, because the output statement is executed immediately after the update, the program still prints this final pair before the loop condition is checked again. Therefore the fourth printed pair is 14 14. Only after printing does the program return to the loop header. At that point the condition m != n becomes false, because 14 equals 14, so the loop terminates and the function returns m, which is 14. If the answer choices present the output as one continuous sequence rather than as four separate pairs, you simply read the printed pairs in order and flatten them. The program prints 42 56, then 42 14, then 28 14, then 14 14. Flattened into a single sequence, this becomes 42 56 42 14 28 14 14 14. This is exactly the sequence that corresponds to option (2), which is why the correct answer for the question is b), matching option (2). It is also useful to understand why the algorithm is correct, because that explains why the final returned value must be the GCD and why the intermediate outputs make sense. Subtracting the smaller number from the larger does not change the set of common divisors. If some integer d divides both m and n, then it also divides m − n (when m > n) because m − n is a linear combination of m and n. Conversely, if d divides m − n and also divides n, then it divides (m − n) + n = m. This means the greatest common divisor is invariant under the transformation (m, n) → (m − n, n) when m > n, and similarly invariant under (m, n) → (m, n − m) when n > m. The loop keeps applying one of these transformations, decreasing the larger value while keeping both values positive, so it must eventually reach a state where m = n. When that equality occurs, the common value must be the greatest common divisor of the original inputs, because the GCD has been preserved at every step and the only common divisor that can equal both values is their greatest common divisor. In this execution, the process reaches 14 14, so the function returns 14, and the printed sequence is simply the trace of these invariant-preserving reductions.