Distinct power numbers: Difference between revisions

no edit summary
No edit summary
Line 403:
4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls,Math}}
Uses Delphi's standard StringList control to avoid duplicates and keep the list storted. It uses a trick to make the strings sorted numerically. The number strings are stored with leading zeros to make them sort numericall and the actual number is stored as an object so it can be retrieved without the leading zeros.
 
<syntaxhighlight lang="Delphi">
 
procedure FindDistinctPowers(Memo: TMemo);
{Display list of numbers a^b sort and exclude duplicates}
{tricks Delphi TStringGrid into sorting numerically}
var A,B,I,P: integer;
var SL: TStringList;
begin
SL:=TStringList.Create;
try
SL.Duplicates:=dupIgnore;
SL.Sorted:=True;
for A:=2 to 5 do
for B:=2 to 5 do
begin
P:=Trunc(Power(A,B));
{Add leading zeros to number so it sorts numerically}
SL.AddObject(FormatFloat('00000',P),Pointer(P));
end;
Memo.Text:=IntToStr(integer(SL.Objects[0]));
for I:=1 to SL.Count-1 do Memo.Text:=Memo.Text+','+IntToStr(integer(SL.Objects[I]));
finally SL.Free; end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
4,8,9,16,25,27,32,64,81,125,243,256,625,1024,3125
 
</pre>
 
 
=={{header|Factor}}==
465

edits