Sort primes from list to a list: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Sort primes from list to a list en BASIC256)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(14 intermediate revisions by 13 users not shown)
Line 12:
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-rows}}
<langsyntaxhighlight 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 #
Line 30:
print( ( newline, " are: " ) );
SHOW ( QUICKSORT prime list FROMELEMENT 1 TOELEMENT p count )[ 1 : p count ]
END</langsyntaxhighlight>
{{out}}
<pre>
Line 36:
are: 2 7 13 43 103
</pre>
 
=={{header|AppleScript}}==
The strangely worded title and task description suggest to this native English speaker that the task is to sort each prime into the primes list ''as it's identified'', which is certainly a less pointless coding exercise than simply extracting all the primes and then sorting them. The implementation here allows for the primes list to be created from scratch or supplied with a few ordered numbers already in it. The sort process is part of an insertion sort.
 
<syntaxhighlight lang="applescript">on isPrime(n)
if (n < 4) then return (n > 1)
if ((n mod 2 is 0) or (n mod 3 is 0)) then return false
repeat with i from 5 to (n ^ 0.5) div 1 by 6
if ((n mod i is 0) or (n mod (i + 2) is 0)) then return false
end repeat
return true
end isPrime
 
-- primes list created from scratch.
on sortPrimesFromList:givenList
return my sortPrimesFromList:givenList toList:{}
end sortPrimesFromList:
 
-- primes list supplied as a parameter, its current contents assumed to be already ordered ascending.
on sortPrimesFromList:givenList toList:primes
set j to (count primes)
repeat with this in givenList
set this to this's contents
if (isPrime(this)) then
set end of primes to this
set j to j + 1
if (j > 1) then
repeat with i from (j - 1) to 1 by -1
set v to primes's item i
if (v > this) then
set primes's item (i + 1) to v
else
set i to i + 1
exit repeat
end if
end repeat
set primes's item i to this
end if
end if
end repeat
return primes
end sortPrimesFromList:toList:
 
on demo()
set primes to my sortPrimesFromList:{2, 43, 81, 22, 63, 13, 7, 95, 103}
log primes
my sortPrimesFromList:{8, 137, 19, 5, 44, 23} toList:primes
log primes
end demo
 
demo()</syntaxhighlight>
 
{{output}}
<pre>Log:
(*2, 7, 13, 43, 103*)
(*2, 5, 7, 13, 19, 23, 43, 103, 137*)</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">lst: [2 43 81 122 63 13 7 95 103]
 
print sort select lst => prime?</syntaxhighlight>
 
{{out}}
 
<pre>2 7 13 43 103</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Primes := [2,43,81,122,63,13,7,95,103]
 
t := [], result := []
for i, n in Primes
if isPrime(n)
t[n, i] := true
for n, obj in t
for i, v in obj
result.push(n)
isPrime(n){
Loop, % floor(sqrt(n))
v := A_Index = 1 ? n : mod(n,A_Index) ? v : v "," A_Index "," n//A_Index
Return (v = n)
}</syntaxhighlight>
{{out}}
<pre>[2, 7, 13, 43, 103]</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SORT_PRIMES_FROM_LIST_TO_A_LIST.AWK
BEGIN {
Line 63 ⟶ 152:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 73 ⟶ 162:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">arraybase 1
global temp
 
Line 129 ⟶ 218:
call sort(temp)
call showArray(temp)
end</langsyntaxhighlight>
{{out}}
<pre>
Line 136 ⟶ 225:
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">Dim Shared As Integer temp()
 
Function isPrime(Byval ValorEval As Integer) As Boolean
Line 177 ⟶ 266:
sort(temp())
showArray(temp())
Sleep</langsyntaxhighlight>
{{out}}
<pre>[2,7,13,43,103]</pre>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">dim Primes(9)
Primes(1) = 2
Primes(2) = 43
Line 235 ⟶ 324:
txt$ = txt$ + "]"
print txt$
end sub</langsyntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses Delphi TList object to hold and sort the data.
 
<syntaxhighlight lang="Delphi">
{Raw data to process}
 
var NumList: array [0..8] of integer = (2,43,81,122,63,13,7,95,103);
 
function Compare(P1,P2: pointer): integer;
{Compare for quick sort}
begin
Result:=Integer(P1)-Integer(P2);
end;
 
procedure GetSortedPrimes(Nums: Array of integer; var IA: TIntegerDynArray);
{Extract data from array "Nums" and return a sorted list of primes}
var I: integer;
var List: TList;
begin
List:=TList.Create;
try
{Put the primes in the TList object}
for I:=0 to High(Nums) do
if IsPrime(Nums[I]) then List.Add(Pointer(Nums[I]));
{Sort the list}
List.Sort(Compare);
{Put the result in array}
SetLength(IA,List.Count);
for I:=0 to List.Count-1 do
IA[I]:=Integer(List[I]);
finally List.Free; end;
end;
 
 
function ArrayToStr(Nums: array of integer): string;
{Convert array of integers to a string}
var I: integer;
begin
Result:='[';
for I:=0 to High(Nums) do
begin
if I<>0 then Result:=Result+',';
Result:=Result+IntToStr(Nums[I]);
end;
Result:=Result+']';
end;
 
 
procedure ShowSortedPrimes(Memo: TMemo);
var I: integer;
var IA: TIntegerDynArray;
var S: string;
begin
GetSortedPrimes(NumList,IA);
Memo.Lines.Add('Raw data: '+ArrayToStr(NumList));
Memo.Lines.Add('Sorted Primes: '+ArrayToStr(IA));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Raw data: [2,43,81,122,63,13,7,95,103]
Sorted Primes: [2,7,13,43,103]
Elapsed Time: 2.910 ms.
</pre>
 
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{out}}
<pre>
Line 254 ⟶ 413:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: math.primes prettyprint sequences sorting ;
 
{ 2 43 81 122 63 13 7 95 103 } [ prime? ] filter natural-sort . </langsyntaxhighlight>
{{out}}
<pre>
{ 2 7 13 43 103 }
</pre>
 
=={{header|Go}}==
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"rcu"
"sort"
)
 
func main() {
list := []int{2, 43, 81, 122, 63, 13, 7, 95, 103}
var primes []int
for _, e := range list {
if rcu.IsPrime(e) {
primes = append(primes, e)
}
}
sort.Ints(primes)
fmt.Println(primes)
}</syntaxhighlight>
 
{{out}}
<pre>
[2 7 13 43 103]
</pre>
 
Line 266 ⟶ 452:
This is a filter (on primality) and a sort (though we could first sort then filter if we preferred):
 
<langsyntaxhighlight Jlang="j"> /:~ (#~ 1&p:)2,43,81,122,63,13,7,95,103
2 7 13 43 103</langsyntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
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</syntaxhighlight>
{{out}}
<pre>
[2,7,13,43,103]
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">julia> using Primes
 
julia> sort(filter(isprime, [2,43,81,122,63,13,7,95,103]))
Line 279 ⟶ 479:
43
103
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Sort[Select[{2, 43, 81, 122, 63, 13, 7, 95, 103}, PrimeQ]]</syntaxhighlight>
{{out}}
<pre>{2, 7, 13, 43, 103}</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[algorithm, strutils]
 
let primes = [2, 43, 81, 122, 63, 13, 7, 95, 103]
echo sorted(primes).join(", ")
</syntaxhighlight>
{{out}}
 
<pre>2, 7, 13, 43, 63, 81, 95, 103, 122
</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Sort_primes_from_list_to_a_list
Line 289 ⟶ 505:
use List::AllUtils qw( nsort_by );
 
print "@{[ nsort_by {$_} grep is_prime($_), 2,43,81,122,63,13,7,95,103 ]}\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 297 ⟶ 513:
=={{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.
<!--<langsyntaxhighlight Phixlang="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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 308 ⟶ 524:
=={{header|Python}}==
===Python: Procedural===
<langsyntaxhighlight lang="python">print("working...")
print("Primes are:")
 
Line 326 ⟶ 542:
Temp.sort()
print(Temp)
print("done...")</langsyntaxhighlight>
{{out}}
<pre>working...
Line 334 ⟶ 550:
 
===Python: Functional===
<langsyntaxhighlight lang="python">'''Prime elements in rising order'''
 
 
Line 377 ⟶ 593:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[2, 7, 13, 43, 103]</pre>
 
=={{header|Quackery}}==
 
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
 
<syntaxhighlight lang="Quackery"> ' [ 2 43 81 122 63 13 7 95 103 ]
sort
dup -1 peek eratosthenes
[] swap witheach
[ dup isprime iff join else drop ]
echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 2 7 13 43 103 ]</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>put <2 43 81 122 63 13 7 95 103>.grep( &is-prime ).sort</langsyntaxhighlight>
{{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" perl6line>put <2 43 81 122 63 13 7 95 103>.grep( &is-prime ).sort: ~*</langsyntaxhighlight>
{{out}}
<pre>103 13 2 43 7</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlibcore.ring"
? "working"
Line 417 ⟶ 648:
txt = txt + "]"
? txt
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 424 ⟶ 655:
[2,7,13,43,103]
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
« SORT { }
1 PICK3 SIZE '''FOR''' j
OVER j GET
'''IF''' DUP ISPRIME? '''THEN''' + '''ELSE''' DROP '''END'''
'''NEXT''' NIP
» '<span style="color:blue">TASK</span>' STO
 
{2,43,81,122,63,13,7,95,103} <span style="color:blue">TASK</span>
{{out}}
<pre>1: { 2 7 13 43 103 }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
p [2,43,81,122,63,13,7,95,103].select(&:prime?).sort</syntaxhighlight>
{{out}}
<pre>[2, 7, 13, 43, 103]
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var arr = [2,43,81,122,63,13,7,95,103]
say arr.grep{.is_prime}.sort</syntaxhighlight>
{{out}}
<pre>
[2, 7, 13, 43, 103]
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">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())</langsyntaxhighlight>
 
{{out}}
Line 439 ⟶ 700:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include xpllib;
int Primes, Smallest, I, SI;
def Len=9, Inf=1000;
Line 451 ⟶ 712:
[IntOut(0, Smallest); ChOut(0, ^ )];
until Smallest = Inf;
]</langsyntaxhighlight>
 
{{out}}
9,485

edits