Sort primes from list to a list: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 12:
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-rows}}
<
PR read "primes.incl.a68" PR # include prime utilities #
PR read "rows.incl.a68" PR # include row (array) utilities #
Line 30:
print( ( newline, " are: " ) );
SHOW ( QUICKSORT prime list FROMELEMENT 1 TOELEMENT p count )[ 1 : p count ]
END</
{{out}}
<pre>
Line 38:
=={{header|AutoHotkey}}==
<
t := [], result := []
Line 53:
v := A_Index = 1 ? n : mod(n,A_Index) ? v : v "," A_Index "," n//A_Index
Return (v = n)
}</
{{out}}
<pre>[2, 7, 13, 43, 103]</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SORT_PRIMES_FROM_LIST_TO_A_LIST.AWK
BEGIN {
Line 84:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 94:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<
global temp
Line 150:
call sort(temp)
call showArray(temp)
end</
{{out}}
<pre>
Line 157:
==={{header|FreeBASIC}}===
<
Function isPrime(Byval ValorEval As Integer) As Boolean
Line 198:
sort(temp())
showArray(temp())
Sleep</
{{out}}
<pre>[2,7,13,43,103]</pre>
==={{header|Yabasic}}===
<
Primes(1) = 2
Primes(2) = 43
Line 256:
txt$ = txt$ + "]"
print txt$
end sub</
{{out}}
<pre>
Line 264:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// Primes from a list. Nigel Galloway: Januuary 23rd., 2022
[2;43;81;122;63;13;7;95;103]|>List.filter isPrime|>List.sort|>List.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 275:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
{ 2 43 81 122 63 13 7 95 103 } [ prime? ] filter natural-sort . </
{{out}}
<pre>
Line 285:
=={{header|Go}}==
{{libheader|Go-rcu}}
<
import (
Line 303:
sort.Ints(primes)
fmt.Println(primes)
}</
{{out}}
Line 314:
This is a filter (on primality) and a sort (though we could first sort then filter if we preferred):
<
2 7 13 43 103</
=={{header|jq}}==
Line 322:
See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.
<
lst | map( select(is_prime) ) | sort</
{{out}}
<pre>
Line 332:
=={{header|Julia}}==
<
julia> sort(filter(isprime, [2,43,81,122,63,13,7,95,103]))
Line 341:
43
103
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{out}}
<pre>{2, 7, 13, 43, 103}</pre>
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Sort_primes_from_list_to_a_list
Line 356:
use List::AllUtils qw( nsort_by );
print "@{[ nsort_by {$_} grep is_prime($_), 2,43,81,122,63,13,7,95,103 ]}\n";</
{{out}}
<pre>
Line 364:
=={{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.
<!--<
<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>
<!--</
{{out}}
<pre>
Line 375:
=={{header|Python}}==
===Python: Procedural===
<
print("Primes are:")
Line 393:
Temp.sort()
print(Temp)
print("done...")</
{{out}}
<pre>working...
Line 401:
===Python: Functional===
<
Line 444:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>[2, 7, 13, 43, 103]</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku"
{{out}}
<pre>2 7 13 43 103</pre>
''Of course "ascending" is a little ambiguous. That ^^^ is numerically. This vvv is lexicographically.
<syntaxhighlight lang="raku"
{{out}}
<pre>103 13 2 43 7</pre>
=={{header|Ring}}==
<
load "stdlibcore.ring"
? "working"
Line 484:
txt = txt + "]"
? txt
</syntaxhighlight>
{{out}}
<pre>
Line 494:
=={{header|Sidef}}==
<
say arr.grep{.is_prime}.sort</
{{out}}
<pre>
Line 503:
=={{header|Wren}}==
{{libheader|Wren-math}}
<
var lst = [2, 43, 81, 122, 63, 13, 7, 95, 103]
System.print(lst.where { |e| Int.isPrime(e) }.toList.sort())</
{{out}}
Line 514:
=={{header|XPL0}}==
<
int Primes, Smallest, I, SI;
def Len=9, Inf=1000;
Line 526:
[IntOut(0, Smallest); ChOut(0, ^ )];
until Smallest = Inf;
]</
{{out}}
|