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

no edit summary
No edit summary
 
(17 intermediate revisions by 13 users not shown)
Line 132:
4 9 16 25 36 36 49 81 121 144 169
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">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]⟩
Task ← ∧∘∾ ⌊⌾√⊸=⊸/¨
 
Task lists</syntaxhighlight>
{{out}}
<pre>⟨ 4 9 16 25 36 36 49 81 121 144 169 ⟩</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,StdCtrls,SysUtils}}
The program uses the Delphi standard "TList" component to collect the numbers. Since it normally holds pointers, the integers are recast as pointers. The TList components provides a quick sort routine that is customized by providing your own compare routine.
 
<syntaxhighlight lang="Delphi">
 
const List1: array [0..8] of integer = (3,4,34,25,9,12,36,56,36);
const List2: array [0..8] of integer = (2,8,81,169,34,55,76,49,7);
const List3: array [0..7] of integer = (75,121,75,144,35,16,46,35);
 
 
function Compare(P1,P2: pointer): integer;
{Compare for quick sort}
begin
Result:=Integer(P1)-Integer(P2);
end;
 
 
procedure SortedSquareNumbers(Memo: TMemo);
{Find and sort numbers that are perfect squares}
var I: integer;
var NumList: TList;
var S: string;
 
procedure LoadList(L: array of integer);
{Load only perfect squares from one of the list }
var I: integer;
begin
for I:=0 to High(L) do
if Sqr(Sqrt(L[I]))=L[I] then
NumList.Add(Pointer(L[I]));
end;
 
begin
NumList:=TList.Create;
try
LoadList(List1);
LoadList(List2);
LoadList(List3);
NumList.Sort(Compare);
S:='';
for I:=0 to NumList.Count-1 do
S:=S+' '+IntToStr(Integer(NumList[I]));
Memo.Lines.Add(S);
finally NumList.Free; end;
end;
</syntaxhighlight>
{{out}}
<pre>
4 9 16 25 36 36 49 55 76 81 121 144 169
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
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 ] ]
#
proc sort . d[] .
for i = 1 to len d[] - 1
for j = i + 1 to len d[]
if d[j] < d[i]
swap d[j] d[i]
.
.
.
.
func issqr x .
return if pow floor sqrt x 2 = x
.
for i to len lists[][]
for e in lists[i][]
if issqr e = 1
sqrs[] &= e
.
.
.
sort sqrs[]
print sqrs[]
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 142 ⟶ 233:
[4; 9; 16; 25; 36; 36; 49; 81; 121; 144; 169]
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99}}
<syntaxhighlight lang="factor">
USING: prettyprint project-euler.common sequences sorting ;
 
{ 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 }
3append [ perfect-square? ] filter sort .
</syntaxhighlight>
{{out}}
<pre>
{ 4 9 16 25 36 36 49 81 121 144 169 }
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">function issquare( n as integer ) as boolean
Line 175 ⟶ 282:
next i</syntaxhighlight>
{{out}}<pre>4 9 16 25 36 36 49 81 121 144 169</pre>
 
 
=={{header|Go}}==
Line 212 ⟶ 320:
[4 9 16 25 36 36 49 81 121 144 169]
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.List (sort)
 
------- PERFECT SQUARE SUBSET OF THREE LISTS SORTED ------
 
squaresSorted :: Integral a => [[a]] -> [a]
squaresSorted = sort . concatMap (filter isPerfectSquare)
 
isPerfectSquare :: Integral a => a -> Bool
isPerfectSquare = (==) <*> ((^ 2) . floor . sqrt . fromIntegral)
 
 
--------------------------- TEST -------------------------
main :: IO ()
main =
print $
squaresSorted
[ [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]
]</syntaxhighlight>
{{Out}}
<pre>[4,9,16,25,36,36,49,81,121,144,169]</pre>
 
=={{header|J}}==
There's many valid approaches here. Here's one of them:
 
<syntaxhighlight lang=J>list1=: 3 4 34 25 9 12 36 56 36
list2=: 2 8 81 169 34 55 76 49 7
list3=: 75 121 75 144 35 16 46 35
 
/:~ (#~ ] = <.&.%:) list1,list2,list3
4 9 16 25 36 36 49 81 121 144 169</syntaxhighlight>
 
=={{header|jq}}==
Line 252 ⟶ 394:
{{out}}<pre>
{4,9,16,25,36,36,49,81,121,144,169}
</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
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]$
block(makelist(sublist(list[i],lambda([x],integerp(sqrt(x)))),i,1,3),map(sort,%%));
</syntaxhighlight>
{{out}}<pre>
[[4,9,25,36,36],[49,81,169],[16,121,144]]
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[algorithm, math, strutils, sugar]
 
func isSquare(n: Natural): bool =
let r = sqrt(n.toFloat).int
result = n == r * r
 
const
List1 = @[3, 4, 34, 25, 9, 12, 36, 56, 36]
List2 = @[2, 8, 81, 169, 34, 55, 76, 49, 7]
List3 = @[75, 121, 75, 144, 35, 16, 46, 35]
 
var squareList = collect:
for list in [List1, List2, List3]:
for n in list:
if n.isSquare:
n
squareList.sort()
 
echo squareList.join(" ")
</syntaxhighlight>
 
{{out}}
<pre>4 9 16 25 36 36 49 81 121 144 169
</pre>
 
Line 307 ⟶ 486:
<pre>REXX-ooRexx_5.0.0(MT)_64-bit 6.05 8 Sep 2020
Ordered squares: 4 9 16 25 36 36 49 81 121 144 169</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Program SortedSquares;
{$mode ObjFPC}{$H+}
 
Uses sysutils,fgl;
 
Type
tmap = specialize TFPGList<integer>;
 
Const List1: array Of integer = (3,4,34,25,9,12,36,56,36);
Const List2: array Of integer = (2,8,81,169,34,55,76,49,7);
Const List3: array Of integer = (75,121,75,144,35,16,46,35);
 
Var list : specialize TFPGList<integer>;
 
Procedure addtolist(arr : Array Of integer);
Var i : integer;
Begin
For i In arr Do
If ((frac(sqrt(i)) = 0) {check if a square, there will be no fraction}
And (list.indexof(i) = -1)) {make sure number isn't in list already}
Then list.add(i);
End;
 
Function CompareInt(Const Item1,Item2: Integer): Integer;
Begin
result := item1 - item2;
End;
 
Var i : integer;
Begin
list := specialize TFPGList<integer>.create;
addtolist(list1);
addtolist(list2);
addtolist(list3);
List.Sort(@CompareInt); {quick sort the list}
For i In list Do
write(i:4);
list.destroy;
End.
</syntaxhighlight>
{{out}}
<pre>
4 9 16 25 36 49 81 121 144 169
</pre>
 
=={{header|Perl}}==
Line 381 ⟶ 608:
done...
</pre>
 
=={{header|Quackery}}==
 
<code>sqrt+</code> is defined at [[Isqrt (integer square root) of X#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ sqrt+ nip 0 = ] is issquare ( n --> b )
 
[]
' [ [ 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 ] ]
witheach
[ witheach
[ dup issquare iff
join else drop ] ]
sort
echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 4 9 16 25 36 36 49 81 121 144 169 ]</pre>
 
=={{header|Raku}}==
Line 506 ⟶ 754:
done...
</pre>
 
=={{header|RPL}}==
≪ + + OBJ→ → <span style="color:green">'''n'''</span>
≪ { } 1 <span style="color:green">'''n'''</span> '''START'''
'''IF''' DUP √ FP '''THEN''' DROP '''ELSE''' + '''END NEXT'''
SORT
≫ ≫ '<span style="color:blue">'''GETSQ'''</span>' STO
 
{ 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 } <span style="color:blue">'''GETSQ''' </span>
'''Output:'''
<span style="color:grey"> 1:</span> { 4 9 16 25 36 36 49 81 121 144 169 }
 
 
=={{header|Ruby}}==
Line 525 ⟶ 785:
<pre>[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
fn main( ) {
let list1 : Vec<i32> = vec![ 3 , 3 , 34 , 25 , 9 , 12 , 36 , 56 , 36] ;
let list2 : Vec<i32> = vec![2, 8 , 81 , 169 , 34 , 55 , 76 , 49 , 7] ;
let list3 : Vec<i32> = vec![ 75 , 121 , 75 , 144 , 35 , 16 , 46 , 35 ] ;
let mut all_numbers : Vec<f32> = Vec::new( ) ;
list1.iter( ).for_each( | n | all_numbers.push( *n as f32 ) ) ;
list2.iter( ).for_each( | n | all_numbers.push( *n as f32)) ;
list3.iter( ).for_each( | n | all_numbers.push( *n as f32) ) ;
let squares : Vec<&f32> = all_numbers.iter( ).filter( | i | {
let root = i.sqrt( ) ;
root == root.floor( )
}).collect( ) ;
let mut found : Vec<i32> = squares.iter( ).map( | n | **n as i32 ).
collect( ) ;
found.sort( ) ;
println!("{:?}" , found ) ;
}</syntaxhighlight>
 
{{out}}
<pre>
[9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var lists = [
Line 541 ⟶ 827:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var lists = [
43

edits