Sum of squares: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎Joy: add)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(20 intermediate revisions by 9 users not shown)
Line 249:
0
4.5</pre>
 
=={{header|ALGOL 60}}==
{{works with|GNU Marst|Any - tested with release 2.7}}
Using [[Jensen's Device]].
<syntaxhighlight lang="algol60">
begin
integer i;
integer array A[ 1 : 5 ];
real procedure sum (i, lo, hi, term);
value lo, hi;
integer i, lo, hi;
real term;
comment term is passed by-name, and so is i;
begin
real temp;
temp := 0;
for i := lo step 1 until hi do
temp := temp + term;
sum := temp
end;
comment initialie A;
for i := 1 step 1 until 5 do A[i] := i;
comment note the correspondence between the mathematical notation and the call to sum;
outreal(1, sum (i, 1, 5, A[i] * A[i]))
end
</syntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 357 ⟶ 383:
 
=={{header|ALGOL W}}==
 
===Using a dedicated "sum of squares" procedure===
 
<syntaxhighlight lang="algolw">begin
% procedure to sum the squares of the elements of a vector. As the procedure can't find %
% the bounds of the arrayvector formust itself,be we pass thempassed in lb and ub %
real procedure sumSquares ( real array vector ( * )
; integer value lb
Line 377 ⟶ 406:
write( sumSquares( numbers, 1, 5 ) );
end.</syntaxhighlight>
 
===Using Jensen's device===
{{Trans|ALGOL60}}
Using the classic [[Jensen's Device]] (first introduced in Algol 60) we can use a generic summation procedure, as in this sample:
 
<syntaxhighlight lang="algolw">
begin % sum the squares of the elements of a vector, using Jensen's Device %
integer i;
real procedure sum ( integer %name% i; integer value lo, hi; real procedure term );
% i is passed by-name, term is passed as a procedure which makes it effectively passed by-name %
begin
real temp;
temp := 0;
i := lo;
while i <= hi do begin % The Algol W "for" loop (as in Algol 68) creates a distinct %
temp := temp + term; % variable which would not be shared with the passed "i" %
i := i + 1 % Here the actual passed "i" is incremented. %
end while_i_le_temp;
temp
end;
real array A ( 1 :: 5 );
for i := 1 until 5 do A( i ) := i;
r_format := "A"; r_w := 10; r_d := 1; % set fixed point output %
write( sum( i, 1, 5, A( i ) * A( i ) ) );
end.
</syntaxhighlight>
 
=={{header|Alore}}==
Line 835 ⟶ 890:
<syntaxhighlight lang="lisp">(defun sum-of-squares (vector)
(loop for x across vector sum (expt x 2)))</syntaxhighlight>
 
Or in a functional way:
<syntaxhighlight lang="lisp">(defun sum-of-squares (vec)
(reduce #'+ (map 'vector (lambda (x) (* x x)) vec)))</syntaxhighlight>
 
=={{header|Cowgol}}==
Line 1,022 ⟶ 1,081:
return sum
}</syntaxhighlight>
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang="easylang">
nums[] = [ 1 2 3 4 5 ]
for v in nums[]
sum += v * v
.
print sum
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 1,060 ⟶ 1,129:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
SumOfSquares(list)
= list.selectBy::(x => x * x).summarize(new Integer());
public program()
Line 1,093 ⟶ 1,162:
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">lists:foldl(fun(X, Sum) -> X*X + Sum end, 0, [3,1,4,1,5,9]).</syntaxhighlight>
 
=={{header|Euler}}==
Using [[Jensen's Device]]
'''begin'''
'''new''' i; '''new''' A; '''new''' sum;
sum &lt;- ` '''formal''' i; '''formal''' lo; '''formal''' hi; '''formal''' term;
'''begin'''
'''new''' temp; '''label''' loop;
temp &lt;- 0;
i &lt;- lo;
loop: '''begin'''
temp &lt;- temp + term;
'''if''' [ i &lt;- i + 1 ] &lt;= hi '''then''' '''goto''' loop '''else''' 0
'''end''';
temp
'''end'''
&apos;;
A &lt;- ( 1, 2, 3, 4, 5 );
'''out''' sum( @i, 1, '''length''' A, `A[i]*A[i]&apos; )
'''end''' $
 
=={{header|Euphoria}}==
Line 1,219 ⟶ 1,309:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sum_of_squares}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Sum of squares 01.png]]
In '''[https://formulae.org/?example=Sum_of_squares this]''' page you can see the program(s) related to this task and their results.
 
'''Test cases'''
 
[[File:Fōrmulæ - Sum of squares 02.png]]
 
[[File:Fōrmulæ - Sum of squares 03.png]]
 
[[File:Fōrmulæ - Sum of squares 04.png]]
 
[[File:Fōrmulæ - Sum of squares 05.png]]
 
[[File:Fōrmulæ - Sum of squares 06.png]]
 
[[File:Fōrmulæ - Sum of squares 07.png]]
 
=={{header|GAP}}==
Line 2,021 ⟶ 2,125:
<pre>[1, 2, 3, 4, 5] → 55
@[] → 0.0</pre>
 
=={{header|Oberon-2}}==
{{trans|Modula-3}}
<syntaxhighlight lang="oberon2">
MODULE SumSquares;
 
IMPORT Out;
 
VAR
A1:ARRAY 6 OF REAL;
 
PROCEDURE Init;
BEGIN
A1[0] := 3.0; A1[1] := 1.0; A1[2] := 4.0; A1[3] := 5.0; A1[4] := 9.0;
END Init;
PROCEDURE SumOfSquares(VAR arr:ARRAY OF REAL):REAL;
VAR
i:LONGINT;
sum:REAL;
BEGIN
sum := 0.0;
FOR i := 0 TO LEN(arr)-1 DO
sum := sum + arr[i] * arr[i]
END;
RETURN sum
END SumOfSquares;
 
BEGIN
Init;
Out.Real(SumOfSquares(A1),0);
Out.Ln
END SumSquares.
</syntaxhighlight>
 
{{out}}
<pre>1.32E+02
</pre>
 
=={{header|Objeck}}==
Line 2,205 ⟶ 2,347:
: (sum '((N) (* N N)) ())
-> 0</syntaxhighlight>
 
=={{header|PL/0}}==
PL/0 has no arrays but they can be simulated.
<syntaxhighlight lang="pascal">
const maxlist = 5;
var sub, v, l1, l2, l3, l4, l5, sum;
procedure getelement;
begin
if sub = 1 then v := l1;
if sub = 2 then v := l2;
if sub = 3 then v := l3;
if sub = 4 then v := l4;
if sub = 5 then v := l5;
end;
procedure setelement;
begin
if sub = 1 then l1 := v;
if sub = 2 then l2 := v;
if sub = 3 then l3 := v;
if sub = 4 then l4 := v;
if sub = 5 then l5 := v;
end;
procedure sumofsquares;
begin
sub := 0;
sum := 0;
while sub < maxlist do begin
sub := sub + 1;
call getelement;
sum := sum + v * v
end;
end;
begin
sub := 0;
while sub < maxlist do begin
sub := sub + 1;
v := sub;
call setelement;
end;
call sumofsquares;
! sum;
end.
</syntaxhighlight>
{{out}}
<pre>
55
</pre>
 
=={{header|PL/I}}==
Line 2,211 ⟶ 2,400:
 
put (sum(A**2));
</syntaxhighlight>
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* CALCULATE THE SUM OF THE SQUARES OF THE ELEMENTS OF AN ARRAY */
 
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
 
/* TASK */
 
/* RETURNS THE SUM OF THE SQUARES OF THE ARRAY AT A$PTR, UB MUST BE THE */
/* UB MUST BE THE UPPER-BOUND OF THE ARRAY */
SUM$OF$SQUARES: PROCEDURE( A$PTR, UB )ADDRESS;
DECLARE ( A$PTR, UB ) ADDRESS;
DECLARE ( I, SUM ) ADDRESS;
DECLARE A BASED A$PTR ( 0 )ADDRESS;
SUM = 0;
DO I = 0 TO UB;
SUM = SUM + ( A( I ) * A( I ) );
END;
RETURN SUM;
END SUM$OF$SQUARES;
 
DECLARE VALUES ( 5 )ADDRESS INITIAL( 1, 2, 3, 4, 5 );
 
CALL PR$NUMBER( SUM$OF$SQUARES( .VALUES, LAST( VALUES ) ) );
 
EOF
</syntaxhighlight>
 
Line 2,432 ⟶ 2,666:
5.25
</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <SquareSum 1 2 3 4 5>>
};
 
SquareSum {
= 0;
s.N e.rest = <+ <* s.N s.N> <SquareSum e.rest>>;
};</syntaxhighlight>
{{out}}
<pre>55</pre>
 
=={{header|ReScript}}==
Line 2,493 ⟶ 2,739:
return sumOfSquares
</syntaxhighlight>
 
=={{header|RPL}}==
Zero-length vectors don't exist in RPL, so there's no need to tackle this case:
≪ DUP DOT ≫ '<span style="color:blue">∑SQV</span>' STO
If we really need zero-length objects, we can use lists:
≪ 0 SWAP
'''IF''' DUP SIZE '''THEN'''
1 OVER SIZE '''FOR''' j
DUP j GET SQ + '''NEXT'''
'''END''' DROP
≫ '<span style="color:blue">∑SQL</span>' STO
Using RPL 1993:
≪ '''IF''' DUP SIZE '''THEN''' SQ ∑LIST '''ELSE''' SIZE '''END''' ≫ '<span style="color:blue">∑SQL</span>' STO
 
[ 1 2 3 4 5 ] <span style="color:blue">∑SQV</span>
{ 1 2 3 4 5 } <span style="color:blue">∑SQL</span>
{ } <span style="color:blue">∑SQL</span>
{{out}}
<pre>
3: 55
2: 55
1: 0
</pre>
 
=={{header|Ruby}}==
Line 2,697 ⟶ 2,966:
(echo 3; echo 1; echo 4;echo 1;echo 5; echo 9) | fold</syntaxhighlight>
 
=={{header|UnixUNIX shellShell}}==
<syntaxhighlight lang="unixshellsh">$sum_squares cat() toto{
_r=0
1
for _n
2
do
4
: "$((_r += _n * _n))"
8
done
16
echo "$_r"
$ cat toto toto | paste -sd*
}
1*2*4*8*16*1*2*4*8*16
 
$ cat toto toto | paste -sd* | bc -l
sum_squares 3 1 4 1 5 9</syntaxhighlight>
1048576
{{out}}
</syntaxhighlight>
<pre>133</pre>
 
=={{header|Ursala}}==
Line 2,819 ⟶ 3,089:
SumOfSquares(15) = 1240
SumOfSquares(16) = 1496
</pre>
 
=={{header|VTL-2}}==
<syntaxhighlight lang="vtl2">
1000 :1)=1
1010 :2)=2
1020 :3)=3
1030 :4)=4
1040 :5)=5
1050 L=5
1060 #=2000
1070 ?=S
1080 #=9999
2000 R=!
2010 S=0
2020 I=1
2030 #=I<L+(I=L)=0*R
2040 S=I*I+S
2050 I=I+1
2060 #=2030
</syntaxhighlight>
{{out}}
<pre>
55
</pre>
 
Line 2,830 ⟶ 3,124:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var sumSquares = Fn.new { |v| v.reduce(0) { |sum, n| sum + n * n } }
 
var v = [1, 2, 3, -1, -2, -3]
9,482

edits