Sum of squares: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
(6 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="eulerlisp">(defun sum-of-squares (vec)
(reduce #'+ (map 'vector (lambda (x) (* x x)) vec)))</syntaxhighlight>
 
=={{header|Cowgol}}==
Line 1,125 ⟶ 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,161 ⟶ 1,165:
=={{header|Euler}}==
Using [[Jensen's Device]]
'''begin'''
<syntaxhighlight lang="euler">
'''new''' i; '''new''' A; '''new''' sum;
begin
sum &lt;- ` '''formal''' i; '''formal''' lo; '''formal''' hi; '''formal''' term;
new i; new A; new sum;
sum <- ` formal i; formal lo; formal hi; formal term;'''begin'''
begin '''new''' temp; '''label''' loop;
new temp; label&lt;- loop0;
temp <i &lt;- 0lo;
loop: i <- lo;'''begin'''
loop: begin temp &lt;- temp + term;
temp <'''if''' [ i &lt;- tempi + term1 ] &lt;= hi '''then''' '''goto''' loop '''else''' 0
if [ i <- i + 1 ] <= hi then goto loop else 0'''end''';
end; temp
temp'''end'''
end&apos;;
';
A &lt;- ( 1, 2, 3, 4, 5 );
 
A <-'''out''' sum( 1@i, 21, 3,'''length''' 4A, 5`A[i]*A[i]&apos; );
'''end''' $
out sum( @i, 1, length A, `A[i]*A[i]' )
end $
</syntaxhighlight>
 
=={{header|Euphoria}}==
Line 1,308 ⟶ 1,310:
 
{{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,646 ⟶ 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,934 ⟶ 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 3,091 ⟶ 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,485

edits