0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OutSystemsで素数判定

Last updated at Posted at 2023-06-23

概要

そのまんま、受け取った数値の素数判定を行うアクションです。
For Eachウィジェットを使わないループのサンプルとしても。

プログラムの構成

Client Action

image.png
image.png

Input Parameter

  • InputNumber
    • Integer
    • ユーザが入力する素数判定対象の整数値。

Output Parameter

  • IsPrimeNumber
    • Boolean
    • 素数判定結果。素数ならTrue、素数でないならFalse

Local Variable

  • LoopCount
    • Integer
    • Default Value: 2
    • ループカウンタ。処理の最初で1までの数は弾いてしまうので開始は2になります。

解説

InputNumber<=1?

素数の定義として「2以上の自然数である」があるので、問答無用で1以下の数値はFalseです。
このためループカウンタのDefault Valueは2になります。

LoopCount<=Sqrt(InputNumber)?

素数の主要な定義は「正の約数が 1 と自分自身のみである」です。なので、1ずつ増やして判定対象を割っていき、判定対象に辿り着くまでに割り切れる数があったらFalse、というのが基本的な考え方です。
が、素数の性質として判定対象まで検査しなくても「判定対象の平方根」まで検査すれば判定可能というものがあります。これに従って判定条件を考えると上記になります。
Sqrt(decimal)はOutSystemsのBuilt-in Functionで引数の平方根を返す関数です。Mathの中にあります。

Mod(InputNumber,LoopCount)=0?

実際の判定部分です。見ての通り、InputNumberLoopCountで割った余りが0ならFalse、0以外ならLoopCountをインクリメントして処理を繰り返します。
LoopCountInputNumberの平方根に届くまで繰り返して一度も割り切れなければTrue、素数と判定します。
ここで、インクリメントした後にもう一度「LoopCount<=Sqrt(InputNumber)?」のIfウィジェットに戻っているところが肝で、これでFor Eachウィジェットを使わないループが実現できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?