Collect and sort square numbers in ascending order from three lists: Difference between revisions
Thundergnat (talk | contribs) m (→{{header|Raku}}: remove a layer of indirection) |
No edit summary |
||
(58 intermediate revisions by 32 users not shown) | |||
Line 3: | Line 3: | ||
;Task: |
;Task: |
||
<br> |
<br> |
||
Collect and sort square numbers in ascending order from three lists. |
|||
<br>list[1] = [3,4,34,25,9,12,36,56,36] |
<br>list[1] = [3,4,34,25,9,12,36,56,36] |
||
<br>list[2] = [2,8,81,169,34,55,76,49,7] |
<br>list[2] = [2,8,81,169,34,55,76,49,7] |
||
<br>list[3] = [75,121,75,144,35,16,46,35] |
<br>list[3] = [75,121,75,144,35,16,46,35] |
||
=={{header|11l}}== |
|||
<syntaxhighlight 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)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169] |
|||
</pre> |
|||
=={{header|ALGOL 68}}== |
|||
{{libheader|ALGOL 68-rows}} |
|||
<syntaxhighlight lang="algol68">BEGIN # construct a list of the squares from some other lists # |
|||
# lists are represented by arrays in this sample # |
|||
PR read "rows.incl.a68" PR # sorting and printing utilities # |
|||
# 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 squares := HEAP[ 1 : 0 ]INT; # start with an empty list # |
|||
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 squares THEN |
|||
# the squares array isn't big enough - make a larger one # |
|||
REF[]INT new squares |
|||
:= HEAP[ 1 : UPB squares + 10 ]INT; |
|||
new squares[ 1 : UPB squares ] := squares; |
|||
squares := new squares |
|||
FI; |
|||
squares[ r pos ] := n |
|||
FI |
|||
OD |
|||
OD; |
|||
QUICKSORT squares FROMELEMENT 1 TOELEMENT r pos; |
|||
SHOW squares[ 1 : r pos ] |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
4 9 16 25 36 36 49 81 121 144 169 |
|||
</pre> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">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: map 1..inc to :integer sqrt max flatten lists 'x -> x^2 |
|||
square?: function [n]-> contains? squares n |
|||
print select sort fold.seed:[] lists [a,b][a++b] => square?</syntaxhighlight> |
|||
{{out}} |
|||
<pre>4 9 16 25 36 36 49 81 121 144 169</pre> |
|||
=={{header|AutoHotkey}}== |
|||
<syntaxhighlight lang="AutoHotkey">list := [], t := [], result := [] |
|||
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] |
|||
for i, l in list |
|||
for j, n in l |
|||
if ((s:=Sqrt(n)) = Floor(s)) |
|||
t[n, j] := true |
|||
for n, obj in t |
|||
for i, v in obj |
|||
result.push(n)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]</pre> |
|||
=={{header|AWK}}== |
|||
<syntaxhighlight lang="AWK"> |
|||
# syntax: GAWK -f COLLECT_AND_SORT_SQUARE_NUMBERS_IN_ASCENDING_ORDER_FROM_THREE_LISTS.AWK |
|||
BEGIN { |
|||
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" |
|||
for (i=1; i<=length(list); i++) { |
|||
n = split(list[i],list_arr,",") |
|||
for (j=1; j<=n; j++) { |
|||
if (is_square(list_arr[j])) { |
|||
sq_arr[i,j] = list_arr[j] |
|||
} |
|||
} |
|||
} |
|||
PROCINFO["sorted_in"] = "@val_num_asc" |
|||
for (i in sq_arr) { |
|||
printf("%d ",sq_arr[i]) |
|||
} |
|||
printf("\n") |
|||
exit(0) |
|||
} |
|||
function is_square(n) { |
|||
return (int(sqrt(n))^2 == n) |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
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#}}== |
|||
<syntaxhighlight lang="fsharp"> |
|||
// Collect and sort square numbers in ascending order from three lists. Nigel Galloway: December 21st., 2021 |
|||
let fN g=g*g in printfn "%A" (([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])|>List.filter(fun n->(float>>sqrt>>int>>fN)n=n)|>List.sort) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[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 |
|||
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</syntaxhighlight> |
|||
{{out}}<pre>4 9 16 25 36 36 49 81 121 144 169</pre> |
|||
=={{header|Go}}== |
|||
<syntaxhighlight lang="go">package main |
|||
import ( |
|||
"fmt" |
|||
"math" |
|||
"sort" |
|||
) |
|||
func isSquare(n int) bool { |
|||
s := int(math.Sqrt(float64(n))) |
|||
return s*s == n |
|||
} |
|||
func main() { |
|||
lists := [][]int{ |
|||
{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 []int |
|||
for _, list := range lists { |
|||
for _, e := range list { |
|||
if isSquare(e) { |
|||
squares = append(squares, e) |
|||
} |
|||
} |
|||
} |
|||
sort.Ints(squares) |
|||
fmt.Println(squares) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[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}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
<syntaxhighlight lang="jq">def isSquare: sqrt | (. == floor);</syntaxhighlight> |
|||
'''The Task''' |
|||
<syntaxhighlight lang="text">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))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[4,9,16,25,36,49,81,121,144,169] |
|||
</pre> |
|||
=={{header|Julia}}== |
|||
vcat is list "addition" in Julia. |
|||
<syntaxhighlight 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) |
|||
</syntaxhighlight> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="Mathematica">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}; |
|||
Sort@Select[Join[list1, list2, list3], IntegerQ[Sqrt[#]] &]</syntaxhighlight> |
|||
{{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> |
|||
=={{header|ooRexx}}== |
|||
<syntaxhighlight lang="oorexx">Parse Version v |
|||
Say v |
|||
l.1=.array~of(3,4,34,25,9,12,36,56,36) |
|||
l.2=.array~of(2,8,81,169,34,55,76,49,7) |
|||
l.3=.array~of(75,121,75,144,35,16,46,35) |
|||
st.0=0 |
|||
Do li=1 To 3 |
|||
Do e over l.li |
|||
If is_square(e) Then |
|||
Call store s |
|||
End |
|||
End |
|||
Call Show |
|||
Exit |
|||
is_square: |
|||
Parse Arg x |
|||
Do i=1 By 1 UNtil i**2>x |
|||
if i**2=x Then Return 1 |
|||
End |
|||
Return 0 |
|||
store: |
|||
do i=1 To st.0 |
|||
/* |
|||
If st.i=e Then |
|||
Return |
|||
*/ |
|||
If st.i>e Then Do |
|||
Do j=st.0 To i By -1 |
|||
ja=j+1 |
|||
st.ja=st.j |
|||
End |
|||
st.0=st.0+1 |
|||
Leave i |
|||
End |
|||
End |
|||
st.i=e |
|||
If i>st.0 Then |
|||
st.0=i |
|||
Return |
|||
show: |
|||
ol='Ordered squares:' |
|||
Do i=1 To st.0 |
|||
ol=ol st.i |
|||
End |
|||
Say ol |
|||
Exit</syntaxhighlight> |
|||
{{out}} |
|||
<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}}== |
|||
{{libheader|ntheory}} |
|||
<syntaxhighlight 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";</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
sum 690 - 4 9 16 25 36 36 49 81 121 144 169 |
|||
</pre> |
|||
=={{header|Phix}}== |
|||
<!--<syntaxhighlight lang="Phix">(phixonline)--> |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">squares</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">lists</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lists</span><span style="color: #0000FF;">)),</span> <span style="color: #000000;">sq</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[$]></span><span style="color: #000000;">sq</span><span style="color: #0000FF;">[$]</span> <span style="color: #008080;">do</span> <span style="color: #000000;">sq</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sq</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"in"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sq</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Added:%,d, Sorted:%V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
<span style="color: #000000;">squares</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span><span style="color: #000000;">56</span><span style="color: #0000FF;">,</span><span style="color: #000000;">36</span><span style="color: #0000FF;">},</span> |
|||
<span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">81</span><span style="color: #0000FF;">,</span><span style="color: #000000;">169</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">55</span><span style="color: #0000FF;">,</span><span style="color: #000000;">76</span><span style="color: #0000FF;">,</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span> |
|||
<span style="color: #0000FF;">{</span><span style="color: #000000;">75</span><span style="color: #0000FF;">,</span><span style="color: #000000;">121</span><span style="color: #0000FF;">,</span><span style="color: #000000;">75</span><span style="color: #0000FF;">,</span><span style="color: #000000;">144</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">46</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">}})</span> |
|||
<!--</syntaxhighlight>--> |
|||
Alternatively, and a smidgen faster but not enough to be measurable here, you could replace that inner while loop with: |
|||
<!--<syntaxhighlight lang="Phix">(phixonline)--> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[$],</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">></span><span style="color: #000000;">s</span> <span style="color: #008080;">do</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">sq</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">;</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
|||
<pre> |
|||
Added:690, Sorted:{4,9,16,25,36,36,49,81,121,144,169} |
|||
</pre> |
|||
You could also apply res:=unique(res), anywhere/8 ways: new line in 3 places, or inline (two ways each) on either existing assignment, or as a replacement for the sort(), which would reduce the length by 1 (the duplicate 36) and yield a total of 654. |
|||
=={{header|Python}}== |
|||
<syntaxhighlight 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...") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
working... |
|||
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169] |
|||
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}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>my $s = cache sort ( my $l = ( cache flat |
||
[3,4,34,25,9,12,36,56,36], |
[3,4,34,25,9,12,36,56,36], |
||
Line 18: | Line 640: | ||
put 'Added - Sorted'; |
put 'Added - Sorted'; |
||
put ' ' ~ $s.sum ~ ' ' ~ $s.gist;</ |
put ' ' ~ $s.sum ~ ' ' ~ $s.gist;</syntaxhighlight> |
||
<pre>Added - Sorted |
<pre>Added - Sorted |
||
690 (4 9 16 25 36 36 49 81 121 144 169) |
690 (4 9 16 25 36 36 49 81 121 144 169) |
||
</pre> |
|||
=={{header|REXX}}== |
|||
<syntaxhighlight lang="rexx">Parse Version v |
|||
Say v |
|||
l.=0 |
|||
Call mk_array 1,3,4,34,25,9,12,36,56,36 |
|||
Call mk_array 2,2,8,81,169,34,55,76,49,7 |
|||
Call mk_array 3,75,121,75,144,3,5,16,46,35 |
|||
st.0=0 |
|||
Do li=1 To 3 |
|||
Do ii=1 To l.li.0 |
|||
If is_square(l.li.ii) Then |
|||
Call store l.li.ii |
|||
End |
|||
End |
|||
Call Show |
|||
Exit |
|||
mk_array: |
|||
an=arg(1) |
|||
Do i=1 To arg()-1 |
|||
l.an.i=arg(i+1) |
|||
End |
|||
l.an.0=arg()-1 |
|||
Return |
|||
is_square: |
|||
Parse Arg x |
|||
Do i=1 By 1 Until i**2>x |
|||
if i**2=x Then Return 1 |
|||
End |
|||
Return 0 |
|||
store: |
|||
Parse Arg e |
|||
do i=1 To st.0 |
|||
If st.i>e Then Do |
|||
Do j=st.0 To i By -1 |
|||
ja=j+1 |
|||
st.ja=st.j |
|||
End |
|||
st.0=st.0+1 |
|||
Leave i |
|||
End |
|||
End |
|||
st.i=e |
|||
If i>st.0 Then |
|||
st.0=i |
|||
Return |
|||
show: |
|||
ol='Ordered squares:' |
|||
Do i=1 To st.0 |
|||
ol=ol st.i |
|||
End |
|||
Say ol |
|||
Exit</syntaxhighlight> |
|||
{{out}} |
|||
<pre>REXX-Regina_3.9.4(MT) 5.00 25 Oct 2021 |
|||
Ordered squares: 4 9 16 25 36 36 49 81 121 144 169 |
|||
</pre> |
</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 64: | Line 747: | ||
txt = txt + "]" |
txt = txt + "]" |
||
see txt |
see txt |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 70: | Line 753: | ||
[4,9,16,25,36,36,49,81,121,144,169] |
[4,9,16,25,36,36,49,81,121,144,169] |
||
done... |
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}}== |
|||
<syntaxhighlight lang="ruby">class Integer |
|||
def square? |
|||
isqrt = Integer.sqrt(self) |
|||
isqrt*isqrt == self |
|||
end |
|||
end |
|||
list = [3,4,34,25,9,12,36,56,36].chain( |
|||
[2,8,81,169,34,55,76,49,7], |
|||
[75,121,75,144,35,16,46,35]) |
|||
p list.select(&:square?).sort |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<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 = [ |
|||
[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], |
|||
] |
|||
say lists.flat.grep{.is_square}.sort</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169] |
|||
</pre> |
</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-math}} |
|||
<lang ecmascript>var lists = [ |
|||
<syntaxhighlight lang="wren">import "./math" for Int |
|||
var lists = [ |
|||
[3,4,34,25,9,12,36,56,36], |
[3,4,34,25,9,12,36,56,36], |
||
[2,8,81,169,34,55,76,49,7], |
[2,8,81,169,34,55,76,49,7], |
||
Line 81: | Line 837: | ||
var squares = [] |
var squares = [] |
||
for (list in lists) { |
for (list in lists) { |
||
for (e in list) |
for (e in list) if (Int.isSquare(e)) squares.add(e) |
||
var s = e.sqrt.floor |
|||
if (s * s == e) squares.add(e) |
|||
} |
|||
} |
} |
||
squares.sort() |
squares.sort() |
||
System.print(squares)</ |
System.print(squares)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<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|XPL0}}== |
|||
The task description seems to dictate how a program is supposed to solve |
|||
this, rather than merely provide a procedure that determines the |
|||
solution. XPL0 doesn't have a built-in list capability like Python. A |
|||
simple substitute is to treat the lists as arrays. A complication is that |
|||
list[3] is shorter than the others, which is adjusted here. XPL0 also doesn't |
|||
have a built-in sort routine (although one is provided in its library). |
|||
Displaying a small number of sorted items is easily done with a few lines |
|||
of code. |
|||
<syntaxhighlight lang="XPL0">int List(3+1), Min, I, J, S, MI, MJ; |
|||
def Inf = -1>>1; |
|||
[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,Inf]; |
|||
loop [Min:= Inf; |
|||
for I:= 1 to 3 do |
|||
for J:= 0 to 8 do |
|||
[S:= sqrt(List(I,J)); |
|||
if S*S = List(I,J) then |
|||
if List(I,J) <= Min then |
|||
[Min:= List(I,J); MI:= I; MJ:= J]; |
|||
]; |
|||
if Min = Inf then quit; |
|||
IntOut(0, Min); ChOut(0, ^ ); |
|||
List(MI, MJ):= Inf; |
|||
]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
4 9 16 25 36 36 49 81 121 144 169 |
|||
</pre> |
</pre> |
Latest revision as of 16:39, 3 April 2024
- Task
Collect 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
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)
- Output:
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
ALGOL 68
BEGIN # construct a list of the squares from some other lists #
# lists are represented by arrays in this sample #
PR read "rows.incl.a68" PR # sorting and printing utilities #
# 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 squares := HEAP[ 1 : 0 ]INT; # start with an empty list #
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 squares THEN
# the squares array isn't big enough - make a larger one #
REF[]INT new squares
:= HEAP[ 1 : UPB squares + 10 ]INT;
new squares[ 1 : UPB squares ] := squares;
squares := new squares
FI;
squares[ r pos ] := n
FI
OD
OD;
QUICKSORT squares FROMELEMENT 1 TOELEMENT r pos;
SHOW squares[ 1 : r pos ]
END
- Output:
4 9 16 25 36 36 49 81 121 144 169
Arturo
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: map 1..inc to :integer sqrt max flatten lists 'x -> x^2
square?: function [n]-> contains? squares n
print select sort fold.seed:[] lists [a,b][a++b] => square?
- Output:
4 9 16 25 36 36 49 81 121 144 169
AutoHotkey
list := [], t := [], result := []
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]
for i, l in list
for j, n in l
if ((s:=Sqrt(n)) = Floor(s))
t[n, j] := true
for n, obj in t
for i, v in obj
result.push(n)
- Output:
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
AWK
# syntax: GAWK -f COLLECT_AND_SORT_SQUARE_NUMBERS_IN_ASCENDING_ORDER_FROM_THREE_LISTS.AWK
BEGIN {
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"
for (i=1; i<=length(list); i++) {
n = split(list[i],list_arr,",")
for (j=1; j<=n; j++) {
if (is_square(list_arr[j])) {
sq_arr[i,j] = list_arr[j]
}
}
}
PROCINFO["sorted_in"] = "@val_num_asc"
for (i in sq_arr) {
printf("%d ",sq_arr[i])
}
printf("\n")
exit(0)
}
function is_square(n) {
return (int(sqrt(n))^2 == n)
}
- Output:
4 9 16 25 36 36 49 81 121 144 169
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
- Output:
⟨ 4 9 16 25 36 36 49 81 121 144 169 ⟩
Delphi
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.
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;
- Output:
4 9 16 25 36 36 49 55 76 81 121 144 169
EasyLang
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[]
F#
// Collect and sort square numbers in ascending order from three lists. Nigel Galloway: December 21st., 2021
let fN g=g*g in printfn "%A" (([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])|>List.filter(fun n->(float>>sqrt>>int>>fN)n=n)|>List.sort)
- Output:
[4; 9; 16; 25; 36; 36; 49; 81; 121; 144; 169]
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 .
- Output:
{ 4 9 16 25 36 36 49 81 121 144 169 }
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
- Output:
4 9 16 25 36 36 49 81 121 144 169
Go
package main
import (
"fmt"
"math"
"sort"
)
func isSquare(n int) bool {
s := int(math.Sqrt(float64(n)))
return s*s == n
}
func main() {
lists := [][]int{
{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 []int
for _, list := range lists {
for _, e := range list {
if isSquare(e) {
squares = append(squares, e)
}
}
}
sort.Ints(squares)
fmt.Println(squares)
}
- Output:
[4 9 16 25 36 36 49 81 121 144 169]
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]
]
- Output:
[4,9,16,25,36,36,49,81,121,144,169]
J
There's many valid approaches here. Here's one of them:
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
jq
Works with gojq, the Go implementation of jq
def isSquare: sqrt | (. == floor);
The Task
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))
- Output:
[4,9,16,25,36,49,81,121,144,169]
Julia
vcat is list "addition" in 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)
Mathematica / Wolfram Language
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};
Sort@Select[Join[list1, list2, list3], IntegerQ[Sqrt[#]] &]
- Output:
{4,9,16,25,36,36,49,81,121,144,169}
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,%%));
- Output:
[[4,9,25,36,36],[49,81,169],[16,121,144]]
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(" ")
- Output:
4 9 16 25 36 36 49 81 121 144 169
ooRexx
Parse Version v
Say v
l.1=.array~of(3,4,34,25,9,12,36,56,36)
l.2=.array~of(2,8,81,169,34,55,76,49,7)
l.3=.array~of(75,121,75,144,35,16,46,35)
st.0=0
Do li=1 To 3
Do e over l.li
If is_square(e) Then
Call store s
End
End
Call Show
Exit
is_square:
Parse Arg x
Do i=1 By 1 UNtil i**2>x
if i**2=x Then Return 1
End
Return 0
store:
do i=1 To st.0
/*
If st.i=e Then
Return
*/
If st.i>e Then Do
Do j=st.0 To i By -1
ja=j+1
st.ja=st.j
End
st.0=st.0+1
Leave i
End
End
st.i=e
If i>st.0 Then
st.0=i
Return
show:
ol='Ordered squares:'
Do i=1 To st.0
ol=ol st.i
End
Say ol
Exit
- Output:
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
Pascal
Free 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.
- Output:
4 9 16 25 36 49 81 121 144 169
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";
- 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}
You could also apply res:=unique(res), anywhere/8 ways: new line in 3 places, or inline (two ways each) on either existing assignment, or as a replacement for the sort(), which would reduce the length by 1 (the duplicate 36) and yield a total of 654.
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...")
- Output:
working... [4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169] done...
Quackery
sqrt+
is defined at Isqrt (integer square root) of X#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
- Output:
[ 4 9 16 25 36 36 49 81 121 144 169 ]
Raku
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;
Added - Sorted 690 (4 9 16 25 36 36 49 81 121 144 169)
REXX
Parse Version v
Say v
l.=0
Call mk_array 1,3,4,34,25,9,12,36,56,36
Call mk_array 2,2,8,81,169,34,55,76,49,7
Call mk_array 3,75,121,75,144,3,5,16,46,35
st.0=0
Do li=1 To 3
Do ii=1 To l.li.0
If is_square(l.li.ii) Then
Call store l.li.ii
End
End
Call Show
Exit
mk_array:
an=arg(1)
Do i=1 To arg()-1
l.an.i=arg(i+1)
End
l.an.0=arg()-1
Return
is_square:
Parse Arg x
Do i=1 By 1 Until i**2>x
if i**2=x Then Return 1
End
Return 0
store:
Parse Arg e
do i=1 To st.0
If st.i>e Then Do
Do j=st.0 To i By -1
ja=j+1
st.ja=st.j
End
st.0=st.0+1
Leave i
End
End
st.i=e
If i>st.0 Then
st.0=i
Return
show:
ol='Ordered squares:'
Do i=1 To st.0
ol=ol st.i
End
Say ol
Exit
- Output:
REXX-Regina_3.9.4(MT) 5.00 25 Oct 2021 Ordered squares: 4 9 16 25 36 36 49 81 121 144 169
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
- Output:
working... [4,9,16,25,36,36,49,81,121,144,169] done...
RPL
≪ + + OBJ→ → n ≪ { } 1 n START IF DUP √ FP THEN DROP ELSE + END NEXT SORT ≫ ≫ 'GETSQ' 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 } GETSQ
Output:
1: { 4 9 16 25 36 36 49 81 121 144 169 }
Ruby
class Integer
def square?
isqrt = Integer.sqrt(self)
isqrt*isqrt == self
end
end
list = [3,4,34,25,9,12,36,56,36].chain(
[2,8,81,169,34,55,76,49,7],
[75,121,75,144,35,16,46,35])
p list.select(&:square?).sort
- Output:
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
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 ) ;
}
- Output:
[9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
Sidef
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],
]
say lists.flat.grep{.is_square}.sort
- Output:
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
Wren
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)
- Output:
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]
XPL0
The task description seems to dictate how a program is supposed to solve this, rather than merely provide a procedure that determines the solution. XPL0 doesn't have a built-in list capability like Python. A simple substitute is to treat the lists as arrays. A complication is that list[3] is shorter than the others, which is adjusted here. XPL0 also doesn't have a built-in sort routine (although one is provided in its library). Displaying a small number of sorted items is easily done with a few lines of code.
int List(3+1), Min, I, J, S, MI, MJ;
def Inf = -1>>1;
[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,Inf];
loop [Min:= Inf;
for I:= 1 to 3 do
for J:= 0 to 8 do
[S:= sqrt(List(I,J));
if S*S = List(I,J) then
if List(I,J) <= Min then
[Min:= List(I,J); MI:= I; MJ:= J];
];
if Min = Inf then quit;
IntOut(0, Min); ChOut(0, ^ );
List(MI, MJ):= Inf;
];
]
- Output:
4 9 16 25 36 36 49 81 121 144 169