Sum of squares: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
(Added PL/0) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(9 intermediate revisions by 6 users not shown) | |||
Line 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,125 ⟶ 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,161 ⟶ 1,165:
=={{header|Euler}}==
Using [[Jensen's Device]]
'''begin'''
'''new''' i; '''new''' A; '''new''' sum;
sum <- ` '''formal''' i; '''formal''' lo; '''formal''' hi; '''formal''' term;
loop:
A <- ( 1, 2, 3, 4, 5 );
'''end''' $
=={{header|Euphoria}}==
Line 1,307 ⟶ 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,384 ⟶ 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,605 ⟶ 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,893 ⟶ 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 3,050 ⟶ 3,124:
=={{header|Wren}}==
<syntaxhighlight lang="
var v = [1, 2, 3, -1, -2, -3]
|