Klarner-Rado sequence: Difference between revisions

no edit summary
(→‎{{header|Phix}}: added faster version (trans Wren))
No edit summary
Line 520:
54381285
1031926801</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Controls,SysUtils,Classes,StdCtrls,ExtCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function SortCompare(Item1, Item2: Pointer): Integer;
{Custom compare to order the list}
begin
Result:=Integer(Item1)-Integer(Item2);
end;
 
procedure KlarnerRadoSeq(Memo: TMemo);
{Display Klarner-Rado sequence}
var LS: TList;
var I,N: integer;
var S: string;
 
procedure AddItem(N: integer);
{Add item to list avoiding duplicates}
begin
if LS.IndexOf(Pointer(N))>=0 then exit;
LS.Add(Pointer(N));
end;
 
function FormatInx(Inx: integer): string;
{Specify an index into the array}
{Returns a formated number}
var D: double;
begin
D:=Integer(LS[Inx]);
Result:=Format('%11.0n',[D]);
end;
 
begin
LS:=TList.Create;
try
{Add string value}
LS.Add(Pointer(1));
{Add two new items to the list}
for I:=0 to high(integer) do
begin
N:=Integer(LS[I]);
AddItem(2 * N + 1);
AddItem(3 * N + 1);
if LS.Count>=100001 then break;
end;
{Put the data in numerical order}
LS.Sort(SortCompare);
{Display data}
S:='[';
for I:=0 to 99 do
begin
if I<>0 then S:=S+' ';
S:=S+Format('%4d',[Integer(LS[I])]);
if (I mod 10)=9 then
begin
if I=99 then S:=S+']';
S:=S+#$0D#$0A;
end;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('The 1,000th '+FormatInx(1000));
Memo.Lines.Add('The 10,000th '+FormatInx(10000));
Memo.Lines.Add('The 100,000th '+FormatInx(100000));
finally LS.Free; end;
end;
</syntaxhighlight>
{{out}}
<pre>
[ 1 3 4 7 9 10 13 15 19 21
22 27 28 31 39 40 43 45 46 55
57 58 63 64 67 79 81 82 85 87
91 93 94 111 115 117 118 121 127 129
130 135 136 139 159 163 165 166 171 172
175 183 187 189 190 193 202 223 231 235
237 238 243 244 247 255 256 259 261 262
271 273 274 279 280 283 319 327 331 333
334 343 345 346 351 352 355 364 367 375
379 381 382 387 388 391 405 406 409 418]
 
The 1,000th 8,488
The 10,000th 157,654
The 100,000th 50,221,174
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
465

edits