掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
ピタゴラス数を列挙するには? (ID:16895)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
諸先輩方の意見に賛成です。 でもやっちゃいました。本能には逆らえません。 感想としては、結構難しかったです。 まだまだ高速化できますが、可読性が悪くなるのでこの辺に留めておきます。 unit Unit1; interface uses Windows, SysUtils, Classes, Controls, Forms, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private function getPythagoras(MaxValue:integer; var count:integer):string; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var str:string; c:Cardinal; MaxValue,count:integer; begin MaxValue:=100; c:=GetTickCount; str:= getPythagoras(MaxValue,count); c:=GetTickCount - c; str:= '算定範囲 = ' + inttostr(MaxValue) + #13#10 + '総数 = ' + inttostr(count) + '個' + #13#10 + '処理にかかった時間 = ' + inttostr(c) + 'ミリ秒' + #13#10 + str; Memo1.Text:=Str; end; function TForm1.getPythagoras(MaxValue: integer; var count:integer): string; // MaxValue:a,bの最大値 // count:総数 var a,b,c,d,m,n: integer; cList : array of integer; function noMuch(index:integer):boolean; var i:integer; begin for i:= 0 to Length(cList)-1 do if cList[i]=index then begin Result:=False; exit; end; SetLength(cList,Length(cList)+1); cList[Length(cList)-1]:=index; Result:=True; end; begin n:=0; repeat inc(n); m:=n; repeat inc(m); a:=(m*m-n*n); b:=(2*m*n) ; c:=(m*m+n*n); d:=0; repeat inc(d); if (a*d <= MaxValue) and (b*d <= MaxValue) and (noMuch(c*d)=True) then if a<b then Result:=Result + inttostr(a*d) +#9+ inttostr(b*d) +#9+ inttostr(c*d) + #13#10 else Result:=Result + inttostr(b*d) +#9+ inttostr(a*d) +#9+ inttostr(c*d) + #13#10 until (a*d > MaxValue) or (b*d > MaxValue); until (a > MaxValue) or (b > MaxValue); m:=n; a:=(m*m-n*n); b:=(2*m*n) ; until (a > MaxValue) or (b > MaxValue); count:=Length(cList); end; end.
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.