Sort primes from list to a list: Difference between revisions

From Rosetta Code
Content added Content deleted
(Undo revision 352353 by Tigerofdarkness (talk) Need to read the task properly...)
(Added Algol 68)
Line 8: Line 8:
<br>
<br>
Show on this page the ascending ordered list of primes from given list.
Show on this page the ascending ordered list of primes from given list.

=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-rows}}
<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>
{{out}}
<pre>
Prime elements of: 2 43 81 122 63 13 7 95 103
are: 2 7 13 43 103
</pre>


=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 13:53, 22 January 2022

Sort primes from list to a list is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
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

Library: ALGOL 68-rows

<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

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...