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