Averages/Root mean square: Difference between revisions

m
(Scala FTW!)
Line 569:
sqrt(sum((x)^2/length(x)))
</lang>
 
=={{header|REXX}}==
REXX has no built-in SQRT, so one is included here.
<lang rexx>
/*REXX program to compute root mean square. */
 
parse arg n . /*get the argument (maybe). */
if n=='' then n=10 /*if not specified, assume ten. */
 
numeric digits 50 /*let's go a little overboard. */
 
sum=0 /*sum of numbers squared (so far)*/
 
do j=1 for n /*step through N integers. */
sum=sum+j**2 /*sum the squares of the integers*/
end
 
rms=sqrt(sum/n) /*divide by N, then get SQRT. */
say 'root mean square for 1-->'n "is" rms /*show & tell.*/
exit
 
 
/*─────────────────────────────────────SQRT subroutine──────────────────*/
sqrt: procedure; parse arg x
if x=0 then return 0 /*handle special case of zero. */
d=digits() /*get the current precision. */
numeric digits digits()+2 /*ensure extra precision. */
g=x/4 /*try get a so-so 1st guesstimate*/
old=0 /*set OLD guess to zero. */
 
do forever
g=.5*(g+x/g) /*do the nitty-gritty calculation*/
if g=old then leave /*if G is the same as old, quit. */
old=g /*save OLD for next iteration. */
end /* .5* is faster than /2 */
 
numeric digits d /*restore the original precision.*/
return g/1 /*normalize to old precision. */
</lang>
Output:
<pre style="height:10ex;overflow:scroll">
root mean square for 1-->10 is 6.2048368229954282980666209777247378499279652953641
</pre>
 
=={{header|Ruby}}==