掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
ピタゴラス数を列挙するには? (ID:16899)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
ご指導ありです。 もっとスマートに解きたいですけどもう疲れました。 気合のある方挑戦してみてはいかがでしょうか? function TForm1.getPythagoras2(MaxValue: integer; var count:integer): string; // MaxValue:a,bの最大値 // count:総数 var a,b,c,d,e,m,n: integer; aList,bList,cList : array of integer; function noMuch(a,b,c:integer):boolean; var i:integer; begin for i:= 0 to Length(cList)-1 do if (cList[i]=c) and (aList[i]=a) and (bList[i]=b) then begin Result:=False; exit; end; i:= Length(cList); SetLength(aList,i+1); SetLength(bList,i+1); SetLength(cList,i+1); aList[i]:=a; bList[i]:=b; cList[i]:=c; 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); if a>b then begin e:=a; a:=b; b:=e; end; d:=0; repeat inc(d); if (a*d <= MaxValue) and (b*d <= MaxValue) and (noMuch(a*d,b*d,c*d)=True) then begin Result:=Result + //inttostr(m) +#9+ //inttostr(n) +#9+ //inttostr(d) +#9+ inttostr(a*d) +#9+ inttostr(b*d) +#9+ inttostr(c*d) +#13#10; end 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;
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.