Sort primes from list to a list: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Quackery.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 6 users not shown)
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}}==
Line 261 ⟶ 329:
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#}}==
Line 347 ⟶ 485:
{{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}}==
Line 506 ⟶ 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>
 
Line 518 ⟶ 689:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var lst = [2, 43, 81, 122, 63, 13, 7, 95, 103]
9,479

edits