Distinct power numbers: Difference between revisions
m
syntax highlighting fixup automation
Drkameleon (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 11:
{{trans|Python}}
<
{{out}}
Line 20:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<
INT FUNC Power(INT a,b)
Line 66:
PrintI(a(i)) Put(32)
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Distinct_power_numbers.png Screenshot from Atari 8-bit computer]
Line 74:
=={{header|Ada}}==
<
with Ada.Containers.Doubly_Linked_Lists;
Line 105:
New_Line;
end Power_Numbers;</
{{out}}
<pre> 4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125 </pre>
=={{header|ALGOL 68}}==
<
INT max number = 5;
INT min number = 2;
Line 146:
OD;
print( ( newline ) )
END</
{{out}}
<pre>
Line 154:
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang
{{out}}
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
Line 161:
===Idiomatic===
Uses an extravagantly long list, but gets the job done quickly and easily.
<
script o
property output : {}
Line 180:
end task
task()</
{{output}}
<
----
Line 189:
Composing a solution from generic primitives, for speed of drafting, and ease of refactoring:
<
use scripting additions
Line 268:
((current application's NSArray's arrayWithArray:xs)'s ¬
sortedArrayUsingSelector:"compare:") as list
end sort</
{{Out}}
<pre>{4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125}</pre>
Line 274:
===AppleScriptObjC===
Throwing together a solution using the most appropriate methods for efficiency and legibility.
<
use framework "Foundation"
Line 291:
end task
task()</
{{output}}
<
=={{header|Arturo}}==
<
map 2..5 'b -> a^b
]</
{{out}}
Line 307:
=={{header|BCPL}}==
<
let pow(n, p) =
Line 335:
if i=0 | v!i ~= v!(i-1) do writef("%N ", v!i)
wrch('*N')
$)</
{{out}}
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
=={{header|BQN}}==
<syntaxhighlight lang
{{out}}
<pre>⟨ 4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125 ⟩</pre>
=={{header|C}}==
<
#include <stdlib.h>
#include <math.h>
Line 371:
printf("\n");
return 0;
}</
{{out}}
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
=={{header|C++}}==
<
#include <set>
#include <cmath>
Line 391:
std::cout << std::endl;
return 0;
}</
{{out}}
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
=={{header|F_Sharp|F#}}==
<
set[for n in 2..5 do for g in 2..5->pown n g]|>Set.iter(printf "%d ")
</syntaxhighlight>
{{out}}
<pre>
Line 406:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
sets sorting ;
2 5 [a,b] dup [ ^ ] cartesian-map concat members natural-sort .</
{{out}}
<pre>
Line 416:
=={{header|FreeBASIC}}==
<
redim arr(-1) as uinteger
dim as uinteger i
Line 434:
if arr(i)<>arr(i-1) then print arr(i),
next i
</syntaxhighlight>
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 473:
}
fmt.Println("\nFound", len(pows), "such numbers.")
}</
{{out}}
Line 486:
=={{header|Haskell}}==
<
Line 501:
main =
print $
distinctPowerNumbers 2 5</
{{Out}}
<pre>[4,8,9,16,25,27,32,64,81,125,243,256,625,1024,3125]</pre>
Line 508:
or, as a one-off list comprehension:
<
main :: IO ()
main =
(print . S.elems . S.fromList) $
(\xs -> [x ^ y | x <- xs, y <- xs]) [2 .. 5]</
or a liftA2 expression:
<
import Control.Monad (join)
import qualified Data.Set as S
Line 525:
join
(liftA2 (^))
[2 .. 5]</
which can always be reduced (shedding imports) to the pattern:
<
main :: IO ()
Line 534:
(print . S.elems . S.fromList) $
(\xs -> (^) <$> xs <*> xs)
[2 .. 5]</
{{Out}}
Line 540:
=={{header|J}}==
<
{{out}}
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
Line 549:
For relatively small integers, such as involved in the specified task, the built-in function `pow/2` does the job:
<
{{out}}
<pre>
[4,8,9,16,25,27,32,64,81,125,243,256,625,1024,3125]
</pre>
However, if using gojq, then for unbounded precision, a special-purpose "power" function is needed:<
[range(2;6) as $a | range(2;6) as $b | $a|power($b)] | unique</
{{out}}
As above.
=={{header|Julia}}==
<
<pre>[4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125]</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{out}}
<pre>{4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125}</pre>
=={{header|Nim}}==
<
let list = collect(newSeq):
Line 575:
for b in 2..5: a^b
echo sorted(list).deduplicate(true).join(" ")</
{{out}}
Line 581:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sqpn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%,5d"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span><span style="color: #000000;">sqpn</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d found:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)})</span>
<!--</
{{out}}
<pre>
Line 596:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Distinct_power_numbers
Line 602:
use List::Util qw( uniq );
print join ', ', sort { $a <=> $b } uniq map { my $e = $_; map $_ ** $e, 2 .. 5} 2 .. 5;</
{{out}}
<pre>
Line 609:
=={{header|Python}}==
<
print(sorted(set(a**b for (a,b) in product(range(2,6), range(2,6)))))</
{{out}}
<pre>[4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125]</pre>
Line 616:
Or, for variation, generalizing a little in terms of '''starmap''' and '''pow''':
<
from itertools import product, starmap
Line 647:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>[4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125]</pre>
Line 653:
=={{header|R}}==
This only takes one line.
<
{{out}}
<pre> [1] 4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku"
{{out}}
<pre>4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125</pre>
Line 664:
=={{header|REXX}}==
With this version of REXX, there's no need to sort the found numbers, or to eliminate duplicates.
<
parse arg lo hi cols . /*obtain optional arguments from the CL*/
if lo=='' | lo=="," then lo= 2 /*Not specified? Then use the default.*/
Line 698:
getMin: parse arg z .; p= 1; #= words($$) /*assume min; # words in $$.*/
do m=2 for #-1; a= word($$, m); if a>=z then iterate; z= a; p= m
end /*m*/; $$= delword($$, p, 1); return /*delete the smallest number.*/</
{{out|output|text= when using the default inputs:}}
<pre>
Line 736:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 769:
see "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 782:
=={{header|Sidef}}==
<
Alternative solution:
<
{{out}}
<pre>
Line 794:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 808:
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.print("$,5d", chunk)
System.print("\nFound %(pows.count) such numbers.")</
{{out}}
Line 821:
=={{header|XPL0}}==
<
[Last:= 0;
loop [Next:= -1>>1; \infinity
Line 833:
Last:= Next;
];
]</
{{out}}
|