Distinct power numbers: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 5 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 577 ⟶ 615:
echo sorted(list).deduplicate(true).join(" ")</syntaxhighlight>
 
{{out}}
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">module IntSet = Set.Make(Int)
 
let pow x =
let rec aux acc b = function
| 0 -> acc
| y -> aux (if y land 1 = 0 then acc else acc * b) (b * b) (y lsr 1)
in
aux 1 x
 
let distinct_powers first count =
let sq = Seq.(take count (ints first)) in
IntSet.of_seq (Seq.map_product pow sq sq)
 
let () = distinct_powers 2 4
(* output *)
|> IntSet.to_seq |> Seq.map string_of_int
|> List.of_seq |> String.concat " " |> print_endline</syntaxhighlight>
{{out}}
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
Line 656 ⟶ 715:
{{out}}
<pre> [1] 4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ [] swap
behead swap
witheach
[ 2dup = iff
drop done
dip join ]
join ] is unique ( [ --> [ )
 
[]
4 times
[ i 2 +
4 times
[ dup i 2 + **
rot join swap ]
drop ]
sort unique echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125 ]</pre>
 
=={{header|Raku}}==
Line 779 ⟶ 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 794 ⟶ 896:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
import "./fmt" for Fmt
 
var pows = []
Line 807 ⟶ 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,476

edits