Sort primes from list to a list: Difference between revisions
(Added Wren) |
|||
Line 35: | Line 35: | ||
Prime elements of: 2 43 81 122 63 13 7 95 103 |
Prime elements of: 2 43 81 122 63 13 7 95 103 |
||
are: 2 7 13 43 103 |
are: 2 7 13 43 103 |
||
</pre> |
|||
=={{header|Phix}}== |
|||
You could also use unique() instead of sort(), since that (by default) performs a sort() internally anyway. It wouldn't be any slower, might even be better, also it does not really make much difference here whether you filter() before or after the sort(), though of course some more expensive filtering operations might be faster given fewer items. |
|||
<!--<lang Phix>(phixonline)--> |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">({</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">43</span><span style="color: #0000FF;">,</span><span style="color: #000000;">81</span><span style="color: #0000FF;">,</span><span style="color: #000000;">122</span><span style="color: #0000FF;">,</span><span style="color: #000000;">63</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">95</span><span style="color: #0000FF;">,</span><span style="color: #000000;">103</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">)))</span> |
|||
<!--</lang>--> |
|||
{{out}} |
|||
<pre> |
|||
{2,7,13,43,103} |
|||
</pre> |
</pre> |
||
Revision as of 17:39, 22 January 2022
- Task
Let given list:
Primes = [2,43,81,122,63,13,7,95,103]
Show on this page the ascending ordered list of primes from given list.
ALGOL 68
<lang algol68>BEGIN # extract the elements of a list that are prime and sort them #
PR read "primes.incl.a68" PR # include prime utilities # PR read "rows.incl.a68" PR # include row (array) utilities # # list of numbers required by the task # []INT list = ( 2, 43, 81, 122, 63, 13, 7, 95, 103 ); [ 1 : UPB list ]INT prime list; # count the nunber of primes in list and assign the primes to prime list # INT p count := 0; FOR i TO UPB list DO IF is probably prime( list[ i ] ) THEN # have a prime # prime list[ p count +:= 1 ] := list[ i ] FI OD; print( ( "prime elements of: " ) ); SHOW list; print( ( newline, " are: " ) ); SHOW ( QUICKSORT prime list FROMELEMENT 1 TOELEMENT p count )[ 1 : p count ]
END</lang>
- Output:
Prime elements of: 2 43 81 122 63 13 7 95 103 are: 2 7 13 43 103
Phix
You could also use unique() instead of sort(), since that (by default) performs a sort() internally anyway. It wouldn't be any slower, might even be better, also it does not really make much difference here whether you filter() before or after the sort(), though of course some more expensive filtering operations might be faster given fewer items.
with javascript_semantics pp(sort(filter({2,43,81,122,63,13,7,95,103},is_prime)))
- Output:
{2,7,13,43,103}
Raku
<lang perl6>put <2 43 81 122 63 13 7 95 103>.grep( &is-prime ).sort</lang>
- Output:
2 7 13 43 103
Ring
<lang ring> load "stdlibcore.ring" ? "working"
Primes = [2,43,81,122,63,13,7,95,103] Temp = []
for n = 1 to len(Primes)
if isprime(Primes[n]) add(Temp,Primes[n]) ok
next
Temp = sort(Temp) ? Temp ? "done..." </lang>
- Output:
working Primes are: 2 7 13 43 103 done...
Wren
<lang ecmascript>import "./math" for Int
var lst = [2, 43, 81, 122, 63, 13, 7, 95, 103] System.print(lst.where { |e| Int.isPrime(e) }.toList.sort())</lang>
- Output:
[2, 7, 13, 43, 103]
XPL0
<lang XPL0>include xpllib; int Primes, Smallest, I, SI; def Len=9, Inf=1000; [Primes:= [2,43,81,122,63,13,7,95,103]; repeat Smallest:= Inf;
for I:= 0 to Len-1 do if Primes(I) < Smallest then [Smallest:= Primes(I); SI:= I]; Primes(SI):= Inf; \cross off if IsPrime(Smallest) then [IntOut(0, Smallest); ChOut(0, ^ )];
until Smallest = Inf; ]</lang>
- Output:
2 7 13 43 103