Distinct power numbers: Difference between revisions
Content added Content deleted
(Added Quackery.) |
(add OCaml) |
||
Line 577: | Line 577: | ||
echo sorted(list).deduplicate(true).join(" ")</syntaxhighlight> |
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}} |
{{out}} |
||
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre> |
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre> |