Distinct power numbers: Difference between revisions

m
(add OCaml)
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 3 users not shown)
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}}==
Line 823 ⟶ 861:
Found 15 numbers
done...
</pre>
 
=={{header|RPL}}==
≪ { }
2 5 '''FOR''' a
2 5 '''FOR''' b
a b ^
'''IF''' DUP2 POS '''THEN''' DROP '''ELSE''' + '''END'''
'''NEXT NEXT''' SORT
≫ '<span style="color:blue">DPOWR</span>' STO
{{out}}
<pre>
1: { 4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125 }
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">a = (2..5).to_a
p a.product(a).map{_1 ** _2}.sort.uniq</syntaxhighlight>
{{out}}
<pre>
[4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125]
</pre>
 
Line 838 ⟶ 896:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
import "./fmt" for Fmt
 
var pows = []
Line 851 ⟶ 909:
pows = Lst.distinct(pows).sort()
System.print("Ordered distinct values of a ^ b for a in [2..5] and b in [2..5]:")
for (chunk in Lst.chunks(pows, 5)) Fmt.printtprint("$,5d", chunkpows, 5)
System.print("\nFound %(pows.count) such numbers.")</syntaxhighlight>
 
9,479

edits