Skip to content

令和8年度 ITパスポート試験 問85

This question's explanation and your answers so far.

問85
頻出度★★☆

関数 isPrime は,引数として与えられた正の整数が,素数であれば true を,素数でなければ false を戻り値とする。例えば,関数 isPrime を isPrime(2) として呼び出したときの戻り値は true である。プログラム中の a,b に入れる字句の適切な組合せはどれか。 〔プログラム〕 ○論理型: isPrime(整数型: num)  整数型: div ← 2  if (num が 2 [ a ])   return false  else   while (num が div [ b ])    if (num ÷ div の余りが 0 と等しい)     return false    else     div ← div + 1    endif   endwhile   return true  endif

出典 令和8年度 ITパスポート試験 問85
解説

問 85

  • Incorrectア

    If a is set to “less than or equal to”, then for num=2 the test “2 is less than or equal to 2” is true and it immediately returns false, contradicting the condition that isPrime(2) is true. Furthermore, b being “equal to” is also wrong: for num=4, “4 equals 2” is false so it does not enter the loop and returns true, wrongly judging the composite number 4 as prime.

  • Incorrectイ

    Although b (greater than) is correct, setting a to “less than or equal to” is wrong. For num=2, “2 is less than or equal to 2” is true and it returns false, failing to satisfy isPrime(2)=true. It rejects the prime number 2 itself.

  • Incorrectウ

    Although a (less than) is correct, setting b to “equal to” is wrong. If the while condition is “num equals div”, then for num=4 with div=2, “4 equals 2” is false so it does not enter the loop and returns true, wrongly judging the composite number 4 as prime. The divisibility check is never performed even once.

  • エ

    正解 The correct answer is エ (a: less than, b: greater than). This is a primality test that increases div from 2 and checks whether num is divisible. First, for a: the initial if exists to “reject values smaller than 2 (such as 1) as non-prime”, and when num=2 it must return true. If a is “less than”, then for num=2 the test “2 is less than 2” is false, so it goes to the else branch and the check proceeds correctly. Next, for b: the while loop increases the divisor div from 2 up to just below num, testing divisibility each time; its condition repeats “until div reaches num=while num is greater than div”. For example, when num=4 and div=2, “4 is greater than 2” is true so it enters the loop, and the remainder of 4÷2 is 0, so it returns false. When num=2, “2 is greater than 2” is false so it does not enter the loop and returns true. When num=3, div=2 gives remainder 1, then div increases to 3 and “3 is greater than 3” is false, so it returns true. All cases are judged correctly.

ここがポイント
  • Primality test algorithm: increase div from 2 up to just below num; if num is divisible at any step it is a composite number (false), and if it is never divisible through the end it is a prime number (true).
  • How to read conditional expressions and boundary values in pseudocode: confirm the range rejected by the if early return (smaller than 2) and the while loop's continuation condition (until div reaches num) by verifying with concrete examples such as isPrime(2)=true.
用語

Your answers