Collect and sort square numbers in ascending order from three lists: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added 11l)
(Added Algol 68)
Line 26: Line 26:
<pre>
<pre>
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
</pre>

=={{header|ALGOL 68}}==
<lang algol68>BEGIN # construct a list of the squares from some other lists #
# lists are represented by arrays in this sample #
# displays a list of numbers #
PROC show = ( []INT list )VOID:
FOR i FROM LWB list TO UPB list DO print( ( " ", whole( list[ i ], 0 ) ) ) OD;
# in-place quick sort an array of integers #
PROC quicksort = ( REF[]INT a, INT lb, ub )VOID:
IF ub > lb THEN
# more than one element, so must sort #
INT left := lb;
INT right := ub;
# choosing the middle element of the array as the pivot #
INT pivot := a[ left + ( ( right + 1 ) - left ) OVER 2 ];
WHILE
WHILE IF left <= ub THEN a[ left ] < pivot ELSE FALSE FI DO left +:= 1 OD;
WHILE IF right >= lb THEN a[ right ] > pivot ELSE FALSE FI DO right -:= 1 OD;
left <= right
DO
INT t := a[ left ];
a[ left ] := a[ right ];
a[ right ] := t;
left +:= 1;
right -:= 1
OD;
quicksort( a, lb, right );
quicksort( a, left, ub )
FI # quicksort # ;

# construct the lists of numbers required by the task #
[][]INT list = ( ( 3, 4, 34, 25, 9, 12, 36, 56, 36 )
, ( 2, 8, 81, 169, 34, 55, 76, 49, 7 )
, ( 75, 121, 75, 144, 35, 16, 46, 35 )
);
# find the elements of the lists that are squares #
INT r pos := 0;
REF[]INT result := HEAP[ 1 : 0 ]INT; # start with an empty list of squares #
FOR i FROM LWB list TO UPB list DO
FOR j FROM LWB list[ i ] TO UPB list[ i ] DO
IF INT n = list[ i ][ j ];
REAL root = sqrt( n );
root = ENTIER root
THEN # have a square #
r pos +:= 1;
IF r pos > UPB result THEN
# the result array isn't big enough - make a larger one #
REF[]INT new result := HEAP[ 1 : UPB result + 10 ]INT;
new result[ 1 : UPB result ] := result;
result := new result
FI;
result[ r pos ] := n
FI
OD
OD;
quicksort( result, 1, r pos ); # sort the squares #
show( result[ 1 : r pos ] )
END</lang>
{{out}}
<pre>
4 9 16 25 36 36 49 81 121 144 169
</pre>
</pre>



Revision as of 13:51, 18 December 2021

Collect and sort square numbers in ascending order from three lists 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


Add and sort square numbers in ascending order from three lists.
list[1] = [3,4,34,25,9,12,36,56,36]
list[2] = [2,8,81,169,34,55,76,49,7]
list[3] = [75,121,75,144,35,16,46,35]

11l

<lang 11l>V lists = [[3, 4, 34, 25, 9, 12, 36, 56, 36], [2, 8, 81, 169, 34, 55, 76, 49, 7], [75, 121, 75, 144, 35, 16, 46, 35]] [Int] squares

F is_square(x)

  R Int(sqrt(x)) ^ 2 == x

L(l) lists

  L(x) l
     I is_square(x)
        squares.append(x)

squares.sort() print(squares)</lang>

Output:
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]

ALGOL 68

<lang algol68>BEGIN # construct a list of the squares from some other lists #

   # lists are represented by arrays in this sample #
   # displays a list of numbers #
   PROC show = ( []INT list )VOID:
       FOR i FROM LWB list TO UPB list DO print( ( " ", whole( list[ i ], 0 ) ) ) OD;
   # in-place quick sort an array of integers #
   PROC quicksort = ( REF[]INT a, INT lb, ub )VOID:
        IF ub > lb THEN
           # more than one element, so must sort #
           INT left    := lb;
           INT right   := ub;
           # choosing the middle element of the array as the pivot #
           INT pivot  := a[ left + ( ( right + 1 ) - left ) OVER 2 ];
           WHILE
               WHILE IF left  <= ub THEN a[ left  ] < pivot ELSE FALSE FI DO left  +:= 1 OD;
               WHILE IF right >= lb THEN a[ right ] > pivot ELSE FALSE FI DO right -:= 1 OD;
               left <= right
           DO
               INT t      := a[ left  ];
               a[ left  ] := a[ right ];
               a[ right ] := t;
               left      +:= 1;
               right     -:= 1
           OD;
           quicksort( a, lb,   right );
           quicksort( a, left, ub    )
        FI # quicksort # ;
   # construct the lists of numbers required by the task #
   [][]INT list = ( (  3,   4, 34,  25,  9, 12, 36, 56, 36 )
                  , (  2,   8, 81, 169, 34, 55, 76, 49,  7 )
                  , ( 75, 121, 75, 144, 35, 16, 46, 35     )         
                  );
   # find the elements of the lists that are squares #
   INT r pos   := 0;
   REF[]INT result := HEAP[ 1 : 0 ]INT; # start with an empty list of squares #
   FOR i FROM LWB list TO UPB list DO
       FOR j FROM LWB list[ i ] TO UPB list[ i ] DO
           IF   INT  n    = list[ i ][ j ];
                REAL root = sqrt( n );
                root = ENTIER root
           THEN # have a square #
                r pos +:= 1;
                IF r pos > UPB result THEN
                    # the result array isn't big enough - make a larger one #
                    REF[]INT new result          := HEAP[ 1 : UPB result + 10 ]INT;
                    new result[ 1 : UPB result ] := result;
                    result                       := new result
                FI;
                result[ r pos ] := n
           FI
       OD
   OD;
   quicksort( result, 1, r pos ); # sort the squares #
   show( result[ 1 : r pos ] )

END</lang>

Output:
 4 9 16 25 36 36 49 81 121 144 169

FreeBASIC

<lang freebasic>function issquare( n as integer ) as boolean

   if int(sqr(n))^2=n then return true else return false

end function

dim as integer i, j, nums(1 to 3, 1 to 9) = {{3,4,34,25,9,12,36,56,36},_

{2,8,81,169,34,55,76,49,7}, {75,121,75,144,35,16,46,35,-1}}, c, d, ii, jj      'pad the last list to make it the same length

redim as integer squares(-1) 'we don't know beforehand how many squares we'll find, so set up a placeholder

while true 'keep going while there are still squares in the lists

   c = 99999                  'sentinel value
   for i = 1 to 3             'loop through lists
       for j = 1 to 9
           d = nums(i, j)
           if issquare(d) then     'have we found a square?
               if d < c then       'is it the smallest so far?
                   c = d
                   ii = i
                   jj = j
               end if
           end if
       next j
   next i
   if c = 99999 then exit while    'if we didn't find a square, stop looking
   redim preserve squares(0 to ubound(squares)+1)      'update our list
   squares(ubound(squares)) = c
   nums(ii,jj) = 13                'mod the original lists so we don't find that same square again

wend

for i = 0 to ubound(squares) 'output data

   print squares(i);"  ";

next i</lang>

Output:
4   9   16   25   36   36   49   81   121   144   169

jq

Works with: jq

Works with gojq, the Go implementation of jq

<lang jq>def isSquare: sqrt | (. == floor);</lang>

The Task <lang>def lists: [

   [3,4,34,25,9,12,36,56,36],
   [2,8,81,169,34,55,76,49,7],
   [75,121,75,144,35,16,46,35]

];

[lists[][]] | unique | map(select(isSquare))</lang>

Output:
[4,9,16,25,36,49,81,121,144,169]

Julia

vcat is list "addition" in Julia. <lang julia>lists = [

   [3,4,34,25,9,12,36,56,36],
   [2,8,81,169,34,55,76,49,7],
   [75,121,75,144,35,16,46,35]

]

squares = reduce(vcat, [[s for s in list if isqrt(s)^2 == s] for list in lists]) sort!(squares) println(squares) </lang>

Perl

Library: ntheory

<lang perl>#!/usr/bin/perl

use strict; # https://rosettacode.org/wiki/Add_and_sort_square_numbers_in_ascending_order_from_three_lists use warnings; use ntheory qw( is_square ); use List::Util qw( sum );

my @lists = (

 [3,4,34,25,9,12,36,56,36],
 [2,8,81,169,34,55,76,49,7],
 [75,121,75,144,35,16,46,35]);

my $sum = sum my @squares = grep is_square($_), sort { $a <=> $b } map @$_, @lists; print "sum $sum - @squares\n";</lang>

Output:
sum 690  -  4 9 16 25 36 36 49 81 121 144 169

Phix

with javascript_semantics
procedure squares(sequence lists)
    sequence res = sort(join(lists)), sq = {1}
    while res[$]>sq[$] do sq &= power(length(sq)+1,2) end while
    res = filter(res,"in",sq)
    printf(1,"Added:%,d, Sorted:%V\n",{sum(res),res})
end procedure

squares({{3,4,34,25,9,12,36,56,36},
         {2,8,81,169,34,55,76,49,7},
         {75,121,75,144,35,16,46,35}})

Alternatively, and a smidgen faster but not enough to be measurable here, you could replace that inner while loop with:

    integer m = res[$], s = 1, d = 1
    while m>s do d += 2; s += d; sq &= s; end while
Output:
Added:690, Sorted:{4,9,16,25,36,36,49,81,121,144,169}

Python

<lang python> import math

print("working...") list = [(3,4,34,25,9,12,36,56,36),(2,8,81,169,34,55,76,49,7),(75,121,75,144,35,16,46,35)] Squares = []

def issquare(x): for p in range(x): if x == p*p: return 1 for n in range(3): for m in range(len(list[n])): if issquare(list[n][m]): Squares.append(list[n][m])

Squares.sort() print(Squares) print("done...") </lang>

Output:
working...
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
done...

Raku

<lang perl6>my $s = cache sort ( my $l = ( cache flat

[3,4,34,25,9,12,36,56,36], [2,8,81,169,34,55,76,49,7], [75,121,75,144,35,16,46,35]

)).grep: * ∈ cache {$++²}…*>$l.max;

put 'Added - Sorted'; put ' ' ~ $s.sum ~ ' ' ~ $s.gist;</lang>

Added - Sorted
 690  (4 9 16 25 36 36 49 81 121 144 169)

Ring

<lang ring> load "stdlib.ring"

see "working..." + nl list = list(3) list[1] = [3,4,34,25,9,12,36,56,36] list[2] = [2,8,81,169,34,55,76,49,7] list[3] = [75,121,75,144,35,16,46,35] Primes = []

for n = 1 to 3

   for m = 1 to len(list[n])
       if issquare(list[n][m])
          add(Primes,list[n][m])
       ok
   next

next

Primes = sort(Primes) showArray(Primes)

see nl + "done..." + nl

func issquare(x)

    for n = 1 to sqrt(x)
        if x = pow(n,2)
           return 1
        ok
    next
    return 0

func showArray(array)

    txt = ""
    see "["
    for n = 1 to len(array)
        txt = txt + array[n] + ","
    next
    txt = left(txt,len(txt)-1)
    txt = txt + "]"
    see txt 

</lang>

Output:
working...
[4,9,16,25,36,36,49,81,121,144,169]
done...

Wren

Library: Wren-math

<lang ecmascript>import "./math" for Int

var lists = [

   [3,4,34,25,9,12,36,56,36],
   [2,8,81,169,34,55,76,49,7],
   [75,121,75,144,35,16,46,35]

]

var squares = [] for (list in lists) {

   for (e in list) if (Int.isSquare(e)) squares.add(e)

} squares.sort() System.print(squares)</lang>

Output:
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]