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.
% the bounds of the
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
<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 <- ` '''formal''' i; '''formal''' lo; '''formal''' hi; '''formal''' term;
'''begin'''
'''new''' temp; '''label''' loop;
temp <- 0;
i <- lo;
loop: '''begin'''
temp <- temp + term;
'''if''' [ i <- i + 1 ] <= hi '''then''' '''goto''' loop '''else''' 0
'''end''';
temp
'''end'''
';
A <- ( 1, 2, 3, 4, 5 );
'''out''' sum( @i, 1, '''length''' A, `A[i]*A[i]' )
'''end''' $
=={{header|Euphoria}}==
Line 1,219 ⟶ 1,309:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sum_of_squares}}
'''Solution'''
[[File:Fōrmulæ - Sum of squares 01.png]]
'''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|
<syntaxhighlight lang="
_r=0
for _n
do
: "$((_r += _n * _n))"
done
echo "$_r"
}
sum_squares 3 1 4 1 5 9</syntaxhighlight>
{{out}}
<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="
var v = [1, 2, 3, -1, -2, -3]
|