パスワードを忘れた? アカウント作成
452740 journal

WindVoiceの日記: 素数判定のための(まったく単純な)Delphiプログラム

日記 by WindVoice

NyaNyaさんに教わったので試してみよう。こんなところにコンボボックスがあるのは忘れていたなぁ。ダウンロードは私のHPから 直結でどうぞ。実行ファイルのほか、ソースコードつきでGPLによる改変および再配布が可能です(笑)

unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, Math;

type
    TForm1 = class(TForm)
        Memo1: TMemo;
        Edit1: TEdit;
        Button1: TButton;
        procedure FormShow(Sender: TObject);
        procedure Button1Click(Sender: TObject);
    end;

var
    Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
    Edit1.Text:=IntToStr(High(Integer));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
    i,n,m: Integer;
    r: Boolean;
begin
    //Edit1の値が素数か確かめる
    n:=StrToInt(Edit1.Text);
    m:=Floor(Sqrt(n));
    r:=True;
    memo1.Text:='判定の最大数:'+IntToStr(m)+#13#10;

    for i:=2 to m do
    begin
        if (n mod i) = 0 then
        begin
            memo1.Text:=IntToStr(i)+'で割れた!'+#13#10+memo1.Text;
            r:=False;
        end;
        if (i mod 1000) = 1 then
        begin
            memo1.Text:=IntToStr(i)+'まで計算した!'+#13#10+memo1.Text;
            Application.ProcessMessages;
        end;
    end;

    if r then
        memo1.Text:=IntToStr(n)+'は素数だった!'+#13#10+memo1.Text
    else
        memo1.Text:=IntToStr(n)+'は合成数だった!'+#13#10+memo1.Text;
end;

end.

この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

吾輩はリファレンスである。名前はまだ無い -- perlの中の人

読み込み中...