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

no edit summary
(Created Nim solution.)
No edit summary
 
(8 intermediate revisions by 7 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}}==
Line 186 ⟶ 196:
</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 196 ⟶ 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 229 ⟶ 282:
next i</syntaxhighlight>
{{out}}<pre>4 9 16 25 36 36 49 81 121 144 169</pre>
 
 
=={{header|Go}}==
Line 340 ⟶ 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>
 
Line 421 ⟶ 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 498 ⟶ 611:
=={{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 )
 
[]
<syntaxhighlight lang="Quackery"> []
' [ [ 3 4 34 25 9 12 36 56 36 ]
[ 2 8 81 169 34 55 76 49 7 ]
Line 712 ⟶ 827:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var lists = [
44

edits