Sum of squares: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|PL/M}}: Correct comment)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 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,307 ⟶ 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'''
 
[[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]]
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 07.png]]
In '''[https://formulae.org/?example=Sum_of_squares this]''' page you can see the program(s) related to this task and their results.
 
=={{header|GAP}}==
Line 2,650 ⟶ 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,938 ⟶ 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,095 ⟶ 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,476

edits