Averages/Root mean square: Difference between revisions

m
m (→‎{{header|REXX}}: added overflow to 'style', changed comments, added comments, added whitespace. -- ~~~~)
imported>Arakov
 
(183 intermediate revisions by 96 users not shown)
Line 1:
{{task}}
Compute the [[wp:Root mean square|Root mean square]] of the numbers 1..10.
 
;Task
The root mean square is also known by its initial RMS (or rms), and as the '''quadratic mean'''.
 
Compute the   [[wp:Root mean square|Root mean square]]   of the numbers 1..10.
 
 
The   ''root mean square''   is also known by its initials RMS (or rms), and as the '''quadratic mean'''.
 
The RMS is calculated as the mean of the squares of the numbers, square-rooted:
: <math>x_{\mathrm{rms}} = \sqrt {{{x_1}^2 + {x_2}^2 + \cdots + {x_n}^2} \over n}. </math>
 
 
Cf. [[Averages/Pythagorean means]]
::: <big><math>x_{\mathrm{rms}} = \sqrt {{{x_1}^2 + {x_2}^2 + \cdots + {x_n}^2} \over n}. </math></big>
 
 
;See also
 
{{Related tasks/Statistical measures}}
 
<br><hr>
 
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F qmean(num)
R sqrt(sum(num.map(n -> n * n)) / Float(num.len))
 
print(qmean(1..10))</syntaxhighlight>
{{out}}
<pre>
6.20484
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
BYTE FUNC Equal(REAL POINTER a,b)
BYTE ARRAY x,y
 
x=a y=b
IF x(0)=y(0) AND x(1)=y(1) AND x(2)=y(2) THEN
RETURN (1)
FI
RETURN (0)
 
PROC Sqrt(REAL POINTER a,b)
REAL z,half
 
IntToReal(0,z)
ValR("0.5",half)
IF Equal(a,z) THEN
RealAssign(z,b)
ELSE
Power(a,half,b)
FI
RETURN
 
PROC Main()
BYTE i
REAL x,x2,sum,tmp
 
IntToReal(0,sum)
FOR i=1 TO 10
DO
IntToReal(i,x)
RealMult(x,x,x2)
RealAdd(sum,x2,tmp)
RealAssign(tmp,sum)
OD
IntToReal(10,x)
RealDiv(sum,x,tmp)
Sqrt(tmp,x)
 
Put(125) PutE() ;clear screen
Print("RMS of 1..10 is ")
PrintRE(x)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Root_mean_square.png Screenshot from Atari 8-bit computer]
<pre>
RMS of 1..10 is 6.20483663
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Numerics.Elementary_Functions;
use Ada.Numerics.Elementary_Functions;
Line 28 ⟶ 103:
list := (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0);
put( rms(list) , Exp=>0);
end calcrms;</langsyntaxhighlight>
{{out}}
Output:
<pre>
6.20484
Line 38 ⟶ 113:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards)}}
<langsyntaxhighlight lang="algol68"># Define the rms PROCedure & ABS OPerators for LONG... REAL #
MODE RMSFIELD = #LONG...# REAL;
PROC (RMSFIELD)RMSFIELD rms field sqrt = #long...# sqrt;
Line 65 ⟶ 140:
print(("crude rms(one to ten): ", crude rms(one to ten), new line));
print(("rms(one to ten): ", rms(one to ten), new line))
)</langsyntaxhighlight>
{{out}}
Output:
<pre>
crude rms(one to ten): +6.20483682299543e +0
rms(one to ten): +6.20483682299543e +0
</pre>
=={{header|ALGOL-M}}==
Because ALGOL-M lacks a built-in square root function, we have to supply our own.
<syntaxhighlight lang="algol">
BEGIN
 
DECIMAL FUNCTION SQRT(X);
DECIMAL X;
BEGIN
DECIMAL R1, R2, TOL;
TOL := .00001; % reasonable for most purposes %
IF X >= 1.0 THEN
BEGIN
R1 := X;
R2 := 1.0;
END
ELSE
BEGIN
R1 := 1.0;
R2 := X;
END;
WHILE (R1-R2) > TOL DO
BEGIN
R1 := (R1+R2) / 2.0;
R2 := X / R1;
END;
SQRT := R1;
END;
 
COMMENT - MAIN PROGRAM BEGINS HERE;
 
DECIMAL N, SQSUM, SQMEAN;
 
SQSUM := 0.0;
FOR N := 1.0 STEP 1.0 UNTIL 10.0 DO
SQSUM := SQSUM + (N * N);
SQMEAN := SQSUM / (N - 1.0);
WRITE("RMS OF WHOLE NUMBERS 1.0 THROUGH 10.0 =", SQRT(SQMEAN));
 
END</syntaxhighlight>
{{out}}
Based on the limited precision of the square root function, only the first six decimal places of the output can actually be relied on (but that's more than sufficient for most real world uses).
<pre>
RMS OF WHOLE NUMBERS 1.0 THROUGH 10.0 = 6.20483683432
</pre>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
% computes the root-mean-square of an array of numbers with %
% the specified lower bound (lb) and upper bound (ub) %
real procedure rms( real array numbers ( * )
; integer value lb
; integer value ub
) ;
begin
real sum;
sum := 0;
for i := lb until ub do sum := sum + ( numbers(i) * numbers(i) );
sqrt( sum / ( ( ub - lb ) + 1 ) )
end rms ;
 
% test the rms procedure with the numbers 1 to 10 %
real array testNumbers( 1 :: 10 );
for i := 1 until 10 do testNumbers(i) := i;
r_format := "A"; r_w := 10; r_d := 4; % set fixed point output %
write( "rms of 1 .. 10: ", rms( testNumbers, 1, 10 ) );
 
end.</syntaxhighlight>
{{out}}
<pre>
rms of 1 .. 10: 6.2048
</pre>
 
=={{header|APL}}==
<langsyntaxhighlight APLlang="apl"> rms←{((+/⍵*2)÷⍴⍵)*0.5}
x←⍳10
 
rms x
6.204836823</langsyntaxhighlight>
 
=={{header|AppleScript}}==
===Functional===
{{Trans|JavaScript}}( ES6 version )
<syntaxhighlight lang="applescript">--------------------- ROOT MEAN SQUARE -------------------
 
-- rootMeanSquare :: [Num] -> Real
on rootMeanSquare(xs)
script
on |λ|(a, x)
a + x * x
end |λ|
end script
(foldl(result, 0, xs) / (length of xs)) ^ (1 / 2)
end rootMeanSquare
 
 
--------------------------- TEST -------------------------
on run
rootMeanSquare({1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
-- > 6.204836822995
end run
 
 
-------------------- GENERIC FUNCTIONS -------------------
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
 
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn</syntaxhighlight>
{{Out}}
<pre>6.204836822995</pre>
----
 
===Straightforward===
<syntaxhighlight lang="applescript">on rootMeanSquare(listOfNumbers)
script o
property lst : listOfNumbers
end script
set r to 0.0
repeat with n in o's lst
set r to r + (n ^ 2)
end repeat
return (r / (count o's lst)) ^ 0.5
end rootMeanSquare
 
rootMeanSquare({1, 2, 3, 4, 5, 6, 7, 8, 9, 10})</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">6.204836822995</syntaxhighlight>
===Integer range alternative===
{{Trans|AutoHotKey}} '''("Avoiding a loop" solution)'''
<syntaxhighlight lang="applescript">
-- RMS of integer range a to b.
on rootMeanSquare(a, b)
return ((b * (b + 1) * (2 * b + 1) - a * (a - 1) * (2 * a - 1)) / 6 / (b - a + 1)) ^ 0.5
end rootMeanSquare
 
rootMeanSquare(1, 10)</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">6.204836822995</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">rootMeanSquare: function [arr]->
sqrt (sum map arr 'i -> i^2) // size arr
 
print rootMeanSquare 1..10</syntaxhighlight>
 
{{out}}
 
<pre>6.204836822995428</pre>
 
=={{header|Astro}}==
<syntaxhighlight lang="python">sqrt(mean(x²))</syntaxhighlight>
 
=={{header|AutoHotkey}}==
===Using a loop===
<langsyntaxhighlight lang="autohotkey">MsgBox, % RMS(1, 10)
 
 
Line 91 ⟶ 336:
Sum += (a + A_Index - 1) ** 2
Return, Sqrt(Sum / n)
}</langsyntaxhighlight>
Message box shows:
<pre>
Line 102 ⟶ 347:
We can show that:<br>
<math>\sum_{i=a}^b i^2 = \frac{b(b+1)(2b+1)-a(a-1)(2a-1)}{6}</math>
<langsyntaxhighlight lang="autohotkey">MsgBox, % RMS(1, 10)
 
 
Line 109 ⟶ 354:
;---------------------------------------------------------------------------
Return, Sqrt((b*(b+1)*(2*b+1)-a*(a-1)*(2*a-1))/6/(b-a+1))
}</langsyntaxhighlight>
Message box shows:
<pre>
Line 116 ⟶ 361:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">#!/usr/bin/awk -f
# computes RMS of the 1st column of a data file
{
Line 126 ⟶ 371:
END {
print "RMS: ",sqrt(S/N);
}</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|QBasic}}
<lang bbcbasic> DIM array(9)
 
Note that this will work in [[Visual Basic]] and the Windows versions of [[PowerBASIC]] by simply wrapping the module-level code into the <code>MAIN</code> function, and changing <code>PRINT</code> to <code>MSGBOX</code>.
 
<syntaxhighlight lang="qbasic">DIM i(1 TO 10) AS DOUBLE, L0 AS LONG
FOR L0 = 1 TO 10
i(L0) = L0
NEXT
PRINT STR$(rms#(i()))
 
FUNCTION rms# (what() AS DOUBLE)
DIM L0 AS LONG, tmp AS DOUBLE, rt AS DOUBLE
FOR L0 = LBOUND(what) TO UBOUND(what)
rt = rt + (what(L0) ^ 2)
NEXT
tmp = UBOUND(what) - LBOUND(what) + 1
rms# = SQR(rt / tmp)
END FUNCTION</syntaxhighlight>
 
See also: [[#BBC BASIC|BBC BASIC]], [[#Liberty BASIC|Liberty BASIC]], [[#PureBasic|PureBasic]], [[#Run BASIC|Run BASIC]]
 
==={{header|Applesoft BASIC}}===
 
<syntaxhighlight lang="applesoftbasic"> 10 N = 10
20 FOR I = 1 TO N
30 S = S + I * I
40 NEXT
50 X = SQR (S / N)
60 PRINT X</syntaxhighlight>
 
{{out}}
<pre>6.20483683</pre>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">precision 8
 
let n = 10
 
for i = 1 to n
 
let s = s + i * i
 
next i
 
print sqrt(s / n)</syntaxhighlight>
{{out| Output}}<pre>6.20483682</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PRINT RMS(10)
110 DEF RMS(N)
120 LET R=0
130 FOR X=1 TO N
140 LET R=R+X^2
150 NEXT
160 LET RMS=SQR(R/N)
170 END DEF</syntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
<syntaxhighlight lang="basic">10 FAST
20 LET RMS=0
30 FOR X=1 TO 10
40 LET RMS=RMS+X**2
50 NEXT X
60 LET RMS=SQR (RMS/10)
70 SLOW
80 PRINT RMS</syntaxhighlight>
{{out}}
<pre>6.2048368</pre>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> DIM array(9)
array() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Line 135 ⟶ 450:
END
DEF FNrms(a()) = MOD(a()) / SQR(DIM(a(),1)+1)</langsyntaxhighlight>
 
=={{header|BQN}}==
RMS is a tacit function which computes root mean square.
<syntaxhighlight lang="bqn">RMS ← √+´∘ט÷≠
 
RMS 1+↕10</syntaxhighlight>
<syntaxhighlight lang="text">6.2048368229954285</syntaxhighlight>
 
[https://mlochbaum.github.io/BQN/try.html#code=Uk1TIOKGkCDiiJorwrTiiJjDl8ucw7fiiaAKClJNUyAxK+KGlTEw Try It!]
 
Another solution is to take the arithmetic mean <code>+´÷≠</code> under the square (<code>ט</code>) function. Under (<code>⌾</code>) squares the arguments, then applies the mean, then inverts the square function.
<syntaxhighlight lang="bqn">RMS ← (+´÷≠)⌾(ט)</syntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 155 ⟶ 482:
printf("%f\n", rms(v, sizeof(v)/sizeof(double)));
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace rms
Line 180 ⟶ 507:
}
}
}</langsyntaxhighlight>
An alternative method demonstrating the more functional style introduced by LINQ and lambda expressions in C# 3.
{{works with|C sharp|C#|3}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 198 ⟶ 525:
private static double rootMeanSquare(IEnumerable<int> x)
{
return Math.Sqrt((double)x.SumAverage(ni => n(double)i * n) / x.Count(i));
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight Cpplang="cpp">#include <iostream>
#include <vector>
#include <cmath>
Line 213 ⟶ 540:
for ( int i = 1 ; i < 11 ; i++ )
numbers.push_back( i ) ;
double meansquare = sqrt ( ( std::inner_product( numbers.begin(), numbers.end(), numbers.begin(), 0 ) ) /10 static_cast<double>( numbers.0size() ) );
std::cout << "The quadratic mean of the numbers 1 .. 10" << numbers.size() << " is " << meansquare << " !\n" ;
return 0 ;
}</langsyntaxhighlight>
{{out}}
Output:
<pre>
The quadratic mean of the numbers 1 .. 10 is 6.20484 !
Line 223 ⟶ 550:
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
<lang clojure>(use '[clojure.contrib.math :only (sqrt)])
 
(defn rms [xs]
(Math/sqrt (/ (reduce + (map #(* % %) xs))
(count xs))))
 
(println (rms (range 1 11)))</langsyntaxhighlight>
{{out}}
Output:
<pre>
6.2048368229954285
</pre>
 
=={{header|COBOL}}==
Could be written more succinctly, with an inline loop and more <tt>COMPUTE</tt> statements; but that wouldn't be very COBOLic.
<syntaxhighlight lang="cobol">IDENTIFICATION DIVISION.
PROGRAM-ID. QUADRATIC-MEAN-PROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 QUADRATIC-MEAN-VARS.
05 N PIC 99 VALUE 0.
05 N-SQUARED PIC 999.
05 RUNNING-TOTAL PIC 999 VALUE 0.
05 MEAN-OF-SQUARES PIC 99V9(16).
05 QUADRATIC-MEAN PIC 9V9(15).
PROCEDURE DIVISION.
CONTROL-PARAGRAPH.
PERFORM MULTIPLICATION-PARAGRAPH 10 TIMES.
DIVIDE RUNNING-TOTAL BY 10 GIVING MEAN-OF-SQUARES.
COMPUTE QUADRATIC-MEAN = FUNCTION SQRT(MEAN-OF-SQUARES).
DISPLAY QUADRATIC-MEAN UPON CONSOLE.
STOP RUN.
MULTIPLICATION-PARAGRAPH.
ADD 1 TO N.
MULTIPLY N BY N GIVING N-SQUARED.
ADD N-SQUARED TO RUNNING-TOTAL.</syntaxhighlight>
{{out}}
<pre>6.204836822995428</pre>
 
=={{header|CoffeeScript}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="coffeescript"> root_mean_square = (ary) ->
sum_of_squares = ary.reduce ((s,x) -> s + x*x), 0
return Math.sqrt(sum_of_squares / ary.length)
alert root_mean_square([1..10])</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(loop for x from 1 to 10
for xx = (* x x)
for n from 1
summing xx into xx-sum
finally (return (sqrt (/ xx-sum n)))))</langsyntaxhighlight>
 
Here's a non-iterative solution.
 
<syntaxhighlight lang="lisp">
(defun root-mean-square (numbers)
"Takes a list of numbers, returns their quadratic mean."
(sqrt
(/ (apply #'+ (mapcar #'(lambda (x) (* x x)) numbers))
(length numbers))))
 
(root-mean-square (loop for i from 1 to 10 collect i))
</syntaxhighlight>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">def rms(seq)
Math.sqrt(seq.sum { |x| x*x } / seq.size)
end
 
puts rms (1..10).to_a</syntaxhighlight>
 
{{out}}
<pre>6.2048368229954285</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.algorithm, std.range;
 
real rms(R)(R d) pure {
return sqrt(d.reduce!((a, b) => a + b * b)(d) / cast(real)(d.length));
}
 
void main() {
writefln("%.19f", rms(iota(1, 11)).rms);
}</langsyntaxhighlight>
{{out}}
Output:
<pre>
6.2048368229954282979
Line 266 ⟶ 641:
 
=={{header|Delphi}}/{{header|Pascal}}==
<langsyntaxhighlight Delphilang="delphi">program AveragesMeanSquare;
 
{$APPTYPE CONSOLE}
Line 287 ⟶ 662:
Writeln(MeanSquare(TDoubleDynArray.Create()));
Writeln(MeanSquare(TDoubleDynArray.Create(1,2,3,4,5,6,7,8,9,10)));
end.</langsyntaxhighlight>
 
=={{header|E}}==
Using the same generic mean function as used in [[../Pythagorean means#E|pythagorean means]]:
<langsyntaxhighlight lang="e">def makeMean(base, include, finish) {
return def mean(numbers) {
var count := 0
Line 303 ⟶ 678:
}
 
def RMS := makeMean(0, fn b,x { b+x**2 }, fn acc,n { (acc/n).sqrt() })</langsyntaxhighlight>
 
<langsyntaxhighlight lang="e">? RMS(1..10)
# value: 6.2048368229954285</langsyntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang=easylang>
func rms v[] .
for v in v[]
sum += v * v
.
return sqrt (sum / len v[])
.
v[] = [ 1 2 3 4 5 6 7 8 9 10 ]
print rms v[]
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
(define (rms xs)
(sqrt (// (for/sum ((x xs)) (* x x)) (length xs))))
 
(rms (range 1 11))
→ 6.2048368229954285
</syntaxhighlight>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 6.x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
import system'math;
extension op
{
get RootMeanSquare()
= (self.selectBy::(x => x * x).summarize(Real.new()) / self.Length).sqrt();
}
public program()
{
console.printLine(new Range(1, 10).RootMeanSquare)
}</syntaxhighlight>
{{out}}
<pre>
6.204836822995
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
defmodule RC do
def root_mean_square(enum) do
enum
|> square
|> mean
|> :math.sqrt
end
 
defp mean(enum), do: Enum.sum(enum) / Enum.count(enum)
 
defp square(enum), do: (for x <- enum, do: x * x)
end
 
IO.puts RC.root_mean_square(1..10)
</syntaxhighlight>
{{out}}
<pre>
6.2048368229954285
</pre>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">(defun rms (nums)
(sqrt (/ (apply '+ (mapcar (lambda (x) (* x x)) nums))
(float (length nums)))))
 
(rms (number-sequence 1 10))</syntaxhighlight>
 
{{out}}
<pre>6.2048368229954285</pre>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">rms(Nums) ->
math:sqrt(lists:foldl(fun(E,S) -> S+E*E end, 0, Nums) / length(Nums)).
 
rms([1,2,3,4,5,6,7,8,9,10]).</langsyntaxhighlight>
{{out}}
Output:
<pre>6.2048368229954285</pre>
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM ROOT_MEAN_SQUARE
BEGIN
N=10
FOR I=1 TO N DO
S=S+I*I
END FOR
X=SQR(S/N)
PRINT("Root mean square is";X)
END PROGRAM
</syntaxhighlight>
You can, obviously, generalize reading data from a DATA line or from a file.
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function rms(sequence s)
atom sum
if length(s) = 0 then
Line 330 ⟶ 795:
 
constant s = {1,2,3,4,5,6,7,8,9,10}
? rms(s)</langsyntaxhighlight>
{{out}}
Output:
<pre>6.204836823</pre>
 
=={{header|Excel}}==
===Cell reference expression===
If values are entered in the cells A1 to A10, the below expression will give the RMS value
<syntaxhighlight lang="excel">
=SQRT(SUMSQ($A1:$A10)/COUNT($A1:$A10))
</syntaxhighlight>
 
The RMS of [1,10] is then : 6.204836823 ( Actual displayed value 6.204837)
 
===LAMBDA===
 
In Excel builds equipped with the LAMBDA function, we can also rework the cell reference expression above to define a custom function, binding a name like ROOTMEANSQR to it in the workBook Name Manager:
 
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
 
{{Works with|Office 365 betas 2021}}
 
<syntaxhighlight lang="lisp">ROOTMEANSQR
=LAMBDA(xs,
SQRT(SUMSQ(xs)/COUNT(xs))
)</syntaxhighlight>
 
For this test, we assume that the following generic lambda is also bound to the name ENUMFROMTO in the Name Manager:
 
<syntaxhighlight lang="lisp">ENUMFROMTO
=LAMBDA(a,
LAMBDA(z,
SEQUENCE(1 + z - a, 1, a, 1)
)
)</syntaxhighlight>
 
{{Out}}
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="2" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=ROOTMEANSQR( ENUMFROMTO( 1 )( 10 ) )
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
|- style="text-align:right;"
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
| style="text-align:right; font-weight:bold" |
| style="font-weight:bold" | [1..10]
|- style="text-align:right;"
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="text-align:right; font-weight:bold" | Root mean square
| style="background-color:#cbcefb;" | 6.2048368229954285
|}
 
=={{header|F Sharp|F#}}==
Uses a lambda expression and function piping.
<langsyntaxhighlight Fsharplang="fsharp">let RMS (x:float list) : float = List.map (fun y -> y**2.0) x |> List.average |> System.Math.Sqrt
 
let res = RMS [1.0..10.0]</langsyntaxhighlight>
Answer (in F# Interactive window):
<pre>val res : float = 6.204836823</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">: root-mean-square ( seq -- mean )
[ [ sq ] map-sum ] [ length ] bi / sqrt ;</syntaxhighlight>
 
( scratchpad ) 10 [1,b] root-mean-square .
6.204836822995428
 
=={{header|Fantom}}==
<langsyntaxhighlight lang="fantom">class Main
{
static Float averageRms (Float[] nums)
Line 358 ⟶ 880:
echo ("RMS Average of $a is: " + averageRms(a))
}
}</langsyntaxhighlight>
 
=={{header|Factor}}==
<lang factor>: root-mean-square ( seq -- mean )
[ [ sq ] map-sum ] [ length ] bi / sqrt ;</lang>
 
( scratchpad ) 10 [1,b] root-mean-square .
6.204836822995428
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: rms ( faddr len -- frms )
dup >r 0e
floats bounds do
Line 376 ⟶ 891:
 
create test 1e f, 2e f, 3e f, 4e f, 5e f, 6e f, 7e f, 8e f, 9e f, 10e f,
test 10 rms f. \ 6.20483682299543</langsyntaxhighlight>
 
=={{header|Fortran}}==
Assume <math> x </math> stored in array x.
<langsyntaxhighlight Fortranlang="fortran">print *,sqrt( sum(x**2)/size(x) )</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
' FB 1.05.0 Win64
 
Function QuadraticMean(array() As Double) As Double
Dim length As Integer = Ubound(array) - Lbound(array) + 1
Dim As Double sum = 0.0
For i As Integer = LBound(array) To UBound(array)
sum += array(i) * array(i)
Next
Return Sqr(sum/length)
End Function
 
Dim vector(1 To 10) As Double
For i As Integer = 1 To 10
vector(i) = i
Next
 
Print "Quadratic mean (or RMS) is :"; QuadraticMean(vector())
Print
Print "Press any key to quit the program"
Sleep
</syntaxhighlight>
 
{{out}}
<pre>
Quadratic mean (or RMS) is : 6.204836822995429
</pre>
 
=={{header|Futhark}}==
 
<syntaxhighlight lang="futhark">
import "futlib/math"
 
fun main(as: [n]f64): f64 =
f64.sqrt ((reduce (+) 0.0 (map (**2.0) as)) / f64(n))
</syntaxhighlight>
 
=={{header|GEORGE}}==
<syntaxhighlight lang="george">
1, 10 rep (i)
i i | (v) ;
0
1, 10 rep (i)
i dup mult +
]
10 div
sqrt
print
</syntaxhighlight>
<pre>
6.204836822995428
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 397 ⟶ 966:
}
fmt.Println(math.Sqrt(sum / n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 405 ⟶ 974:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def quadMean = { list ->
list == null \
? null \
Line 411 ⟶ 980:
? 0 \
: ((list.collect { it*it }.sum()) / list.size()) ** 0.5
}</langsyntaxhighlight>
Test:
<langsyntaxhighlight lang="groovy">def list = 1..10
def Q = quadMean(list)
println """
list: ${list}
Q: ${Q}
"""</langsyntaxhighlight>
{{out}}
Output:
<pre>list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Q: 6.2048368229954285</pre>
 
=={{header|Haskell}}==
Given the <code>mean</code> function defienddefined in [[Averages/Pythagorean means]]:
<langsyntaxhighlight lang="haskell">main = print $ mean 2 [1 .. 10]</langsyntaxhighlight>
 
Or, writing a naive '''mean''' of our own, (but see https://donsbot.wordpress.com/2008/06/04/haskell-as-fast-as-c-working-at-a-high-altitude-for-low-level-performance/):
 
<syntaxhighlight lang="haskell">import Data.List (genericLength)
 
rootMeanSquare :: [Double] -> Double
rootMeanSquare = sqrt . (((/) . foldr ((+) . (^ 2)) 0) <*> genericLength)
 
main :: IO ()
main = print $ rootMeanSquare [1 .. 10]</syntaxhighlight>
{{Out}}
<pre>6.2048368229954285</pre>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">sum = 0
DO i = 1, 10
sum = sum + i^2
ENDDO
WRITE(ClipBoard) "RMS(1..10) = ", (sum/10)^0.5 </langsyntaxhighlight>
RMS(1..10) = 6.204836823
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main()
every put(x := [], 1 to 10)
writes("x := [ "); every writes(!x," "); write("]")
write("Quadratic mean:",q := qmean!x)
end</langsyntaxhighlight>
 
 
<langsyntaxhighlight Iconlang="icon">procedure qmean(L[]) #: quadratic mean
local m
if *L = 0 then fail
every (m := 0.0) +:= !L^2
return sqrt(m / *L)
end</langsyntaxhighlight>
 
=={{header|Io}}==
<langsyntaxhighlight Iolang="io">rms := method (figs, (figs map(** 2) reduce(+) / figs size) sqrt)
 
rms( Range 1 to(10) asList ) println</langsyntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">rms=: (+/ % #)&.:*:</langsyntaxhighlight>
 
'''Example Usage:'''
<langsyntaxhighlight lang="j"> rms 1 + i. 10
6.20484</langsyntaxhighlight>
<code>*:</code> means [http://jsoftware.com/help/dictionary/d112.htm square]
 
Line 469 ⟶ 1,050:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class RMSRootMeanSquare {
 
public static double rms(double[] nums){
public static double rootMeanSquare(double... ms =nums) 0;{
fordouble (int isum = 0; i < nums.length0; i++)
for (double num ms += nums[i] *: nums[i];)
ms / sum += nums.lengthnum * num;
return Math.sqrt(mssum / nums.length);
}
 
public static void main(String[] args) {
double[] nums = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
System.out.println("The RMS of the numbers from 1 to 10 is " + rmsrootMeanSquare(nums));
}
}</langsyntaxhighlight>
{{out}}
Output:
<pre>The RMS of the numbers from 1 to 10 is 6.2048368229954285</pre>
 
=={{header|JavaScript}}==
===ES5===
{{works with|JavaScript|1.8}},
{{works with|JavaScript|1.8}}
{{works with|Firefox|3.0}}
<langsyntaxhighlight lang="javascript">function root_mean_square(ary) {
var sum_of_squares = ary.reduce(function(s,x) {return (s + x*x)}, 0);
return Math.sqrt(sum_of_squares / ary.length);
}
 
print( root_mean_square([1,2,3,4,5,6,7,8,9,10]) ); // ==> 6.2048368229954285</langsyntaxhighlight>
 
 
===ES6===
 
<syntaxhighlight lang="javascript">(() => {
'use strict';
// rootMeanSquare :: [Num] -> Real
const rootMeanSquare = xs =>
Math.sqrt(
xs.reduce(
(a, x) => (a + x * x),
0
) / xs.length
);
return rootMeanSquare([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// -> 6.2048368229954285
})();</syntaxhighlight>
 
{{Out}}
<pre>6.2048368229954285</pre>
 
=={{header|jq}}==
The following filter returns ''null'' if given an empty array:
<syntaxhighlight lang="jq">def rms: length as $length
| if $length == 0 then null
else map(. * .) | add | sqrt / $length
end ;</syntaxhighlight>With this definition, the following program would compute the rms of each array in a file or stream of numeric arrays:<syntaxhighlight lang="jq">rms</syntaxhighlight>
 
=={{header|Julia}}==
There are a variety of ways to do this via built-in functions in Julia, given an array <code>A = [1:10]</code> of values. The formula can be implemented directly as:
<syntaxhighlight lang="julia">sqrt(sum(A.^2.) / length(A))</syntaxhighlight>
or shorter with using Statistics (and as spoken: root-mean-square)
<syntaxhighlight lang="julia">sqrt(mean(A.^2.))</syntaxhighlight>
or the implicit allocation of a new array by <code>A.^2.</code> can be avoided by using <code>sum</code> as a higher-order function: <syntaxhighlight lang="julia">sqrt(sum(x -> x*x, A) / length(A))</syntaxhighlight>
One can also use an explicit loop for near-C performance
<syntaxhighlight lang="julia">
function rms(A)
s = 0.0
for a in A
s += a*a
end
return sqrt(s / length(A))
end
</syntaxhighlight>
Potentially even better is to use the built-in <code>norm</code> function, which computes the square root of the sum of the squares of the entries of <code>A</code> in a way that avoids the possibility of spurious floating-point overflow (if the entries of <code>A</code> are so large that they may overflow if squared): <syntaxhighlight lang="julia">norm(A) / sqrt(length(A))</syntaxhighlight>
 
=={{header|K}}==
<syntaxhighlight lang="k">
rms:{_sqrt (+/x^2)%#x}
rms 1+!10
6.204837
</syntaxhighlight>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.5-2
 
fun quadraticMean(vector: Array<Double>) : Double {
val sum = vector.sumByDouble { it * it }
return Math.sqrt(sum / vector.size)
}
fun main(args: Array<String>) {
val vector = Array(10, { (it + 1).toDouble() })
print("Quadratic mean of numbers 1 to 10 is ${quadraticMean(vector)}")
}</syntaxhighlight>
 
{{out}}
<pre>
Quadratic mean of numbers 1 to 10 is 6.2048368229954285
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def rms
{lambda {:n}
{sqrt
{/ {+ {S.map {lambda {:i} {* :i :i}}
{S.serie 1 :n}}}
:n}}}}
-> rms
 
{rms 10}
-> 6.2048368229954285
</syntaxhighlight>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">define rms(a::staticarray)::decimal => {
return math_sqrt((with n in #a sum #n*#n) / decimal(#a->size))
}
rms(generateSeries(1,10)->asStaticArray)</syntaxhighlight>
 
{{out}}
<pre>6.204837</pre>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">' [RC] Averages/Root mean square
 
SourceList$ ="1 2 3 4 5 6 7 8 9 10"
Line 522 ⟶ 1,202:
print "R.M.S. value is "; ( SumOfSquares /n)^0.5
 
end</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to rms :v
output sqrt quotient (apply "sum map [? * ?] :v) count :v
end
 
show rms iseq 1 10</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function sumsq(a, ...) return a and a^2 + sumsq(...) or 0 end
function rms(t) return (sumsq(unpack(t)) / #t)^.5 end
 
print(rms{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})</langsyntaxhighlight>
 
=={{header|MathematicaMaple}}==
<syntaxhighlight lang="maple">y := [ seq(1..10) ]:
<lang Mathematica>RootMeanSquare@Range[10]</lang>
RMS := proc( x )
return sqrt( Statistics:-Mean( x ^~ 2 ) );
end proc:
RMS( y );
</syntaxhighlight>
{{out}}
<pre>6.20483682299543
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">RootMeanSquare@Range[10]</syntaxhighlight>
The above will give the precise solution <math>\sqrt{\frac{77}{2}}</math>, to downgrade to 6.20484, use '<code>10.</code>' to imply asking for numeric solution, or append '<code>//N</code>' after the whole expression.
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function rms = quadraticMean(list)
rms = sqrt(mean(list.^2));
end</langsyntaxhighlight>
Solution:
<langsyntaxhighlight MATLABlang="matlab">>> quadraticMean((1:10))
 
ans =
 
6.204836822995429</langsyntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang ="maxima">L: makelist(i, i, 1, 10)$
 
rms(L) := sqrt(lsum(x^2, x, L)/length(L))$
 
rms(L), numer; /* 6.204836822995429 */</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
fn RMS arr =
(
local sumSquared = 0
for i in arr do sumSquared += i^2
return (sqrt (sumSquared/arr.count as float))
)
</syntaxhighlight>
Output:
<syntaxhighlight lang="maxscript">
rms #{1..10}
6.20484
</syntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.37.0}}
<syntaxhighlight lang="min">(((dup *) map sum) keep size / sqrt) ^rms
 
(1 2 3 4 5 6 7 8 9 10) rms puts!</syntaxhighlight>
{{out}}
<pre>6.204836822995428</pre>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">0 П0 П1 С/П x^2 ИП0 x^2 ИП1 *
+ ИП1 1 + П1 / КвКор П0 БП
03</syntaxhighlight>
 
''Instruction:'' В/О С/П Number С/П Number ...
 
Each time you press the С/П on the indicator would mean already entered numbers.
 
=={{header|Morfa}}==
{{trans|D}}
<syntaxhighlight lang="morfa">
import morfa.base;
import morfa.functional.base;
 
template <TRange>
func rms(d: TRange): float
{
var count = 1;
return sqrt(reduce( (a: float, b: float) { count += 1; return a + b * b; }, d) / count);
}
func main(): void
{
println(rms(1 .. 11));
}
</syntaxhighlight>
{{out}}
<pre>
6.204837
</pre>
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
using System.Math;
Line 575 ⟶ 1,322:
WriteLine("RMS of [1 .. 10]: {0:g6}", RMS($[1 .. 10]));
}
}</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
parse arg maxV .
if maxV = '' | maxV = '.' then maxV = 10
 
sum = 0
loop nr = 1 for maxV
sum = sum + nr ** 2
end nr
rmsD = Math.sqrt(sum / maxV)
 
say 'RMS of values from 1 to' maxV':' rmsD
 
return
</syntaxhighlight>
{{out}}
<pre>
RMS of values from 1 to 10: 6.204836822995428
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">from math import sqrt, sum
from sequtils import mapIt
proc qmean(num: seq[float]): float =
result = num.mapIt(it * it).sum
result = sqrt(result / float(num.len))
echo qmean(@[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0])</syntaxhighlight>
{{out}}
<pre>6.204836822995428</pre>
 
=={{header|Oberon-2}}==
Oxford Oberon-2
<langsyntaxhighlight lang="oberon2">
MODULE QM;
IMPORT ML := MathL, Out;
Line 604 ⟶ 1,385:
Out.String("Quadratic Mean: ");Out.LongReal(Rms(nums));Out.Ln
END QM.
</syntaxhighlight>
</lang>
{{out}}
Output:
<pre>
Quadratic Mean: 6.20483682300
</pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">bundle Default {
class Hello {
function : Main(args : String[]) ~ Nil {
Line 627 ⟶ 1,409:
}
}
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let rms a =
sqrt (Array.fold_left (fun s x -> s +. x*.x) 0.0 a /.
float_of_int (Array.length a))
Line 636 ⟶ 1,418:
 
rms (Array.init 10 (fun i -> float_of_int (i+1))) ;;
(* 6.2048368229954285 *)</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<syntaxhighlight lang="oforth">10 seq map(#sq) sum 10.0 / sqrt .</syntaxhighlight>
 
{{out}}
<pre>
6.20483682299543
</pre>
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">call testAverage .array~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
<lang ooRexx>
call testAverage .array~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
call testAverage .array~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, .11)
call testAverage .array~of(30, 10, 20, 30, 40, 50, -100, 4.7, -11e2)
Line 660 ⟶ 1,451:
return rxcalcsqrt(sum/numbers~items)
 
::requires rxmath LIBRARY</syntaxhighlight>
{{out}}
</lang>
<pre>list = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
root mean square = 6.20483682
 
list = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, .11
root mean square = 5.06630766
 
list = 30, 10, 20, 30, 40, 50, -100, 4.7, -1100
root mean square = 369.146476</pre>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
fun {Square X} X*X end
 
Line 674 ⟶ 1,473:
end
in
{Show {RMS {List.number 1 10 1}}}</langsyntaxhighlight>
{{out}}
Output:
<pre>
6.2048
Line 682 ⟶ 1,481:
=={{header|PARI/GP}}==
General RMS calculation:
<langsyntaxhighlight lang="parigp">RMS(v)={
sqrt(sum(i=1,#v,v[i]^2)/#v)
};
 
RMS(vector(10,i,i))</langsyntaxhighlight>
 
Specific functions for the first ''n'' positive integers:
<langsyntaxhighlight lang="parigp">RMS_first(n)={
sqrt((n+1)*(2*n+1)/6)
};
 
RMS_first(10)</langsyntaxhighlight>
Asymptotically this is n/sqrt(3).
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use v5.10.0;
sub rms
{
my $r = 0;
$r += $_**2 for @_;
return sqrt( $r/@_ );
}
 
say rms(1..10);</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang perl6>sub rms(*@nums) { sqrt [+](@nums X** 2) / @nums }
<span style="color: #008080;">function</span> <span style="color: #000000;">rms</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">sqsum</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">sqsum</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sqsum</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">rms</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
6.204836823
</pre>
Alternative, same output<br>
You could make this a one-liner, for no gain and making it harder to debug - an explicitly named intermediate such as sqsum adds no additional overhead compared to the un-named hidden temporary variable the compiler would otherwise use anyway, and of course sqsum can be examined/verified to pinpoint any error more effectively. The last (commented-out) line also removes the function call, but of course it has also removed every last descriptive hint of what it is supposed to be doing as well.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">rms</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">sqsum</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}))</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sqsum</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">rms</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
<span style="color: #000080;font-style:italic;">-- ?sqrt(sum(apply(true,power,{tagset(10),2}))/10) -- (ugh)</span>
<!--</syntaxhighlight>-->
 
=={{header|Phixmonti}}==
say rms 1..10;</lang>
<syntaxhighlight lang="phixmonti">def rms
0 swap
len for
get 2 power rot + swap
endfor
len rot swap / sqrt
enddef
 
0 tolist
=={{header|PL/I}}==
10 for
<lang PL/I>declare A(10) fixed decimal static initial (1,2,3,4,5,6,7,8,9,10);
0 put
n = hbound(A,1);
endfor
RMS = sqrt(sum(A**2)/n);</lang>
rms print</syntaxhighlight>
 
=={{header|PHP}}==
<syntaxhighlight lang="php"><?php
// Created with PHP 7.0
 
function rms(array $numbers)
{
$sum = 0;
 
foreach ($numbers as $number) {
$sum += $number**2;
}
 
return sqrt($sum / count($numbers));
}
 
echo rms(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
</syntaxhighlight>
{{out}}
<pre>
6.2048368229954
</pre>
 
=={{header|Picat}}==
{{trans|Prolog}}
{{works with|Picat}}
<syntaxhighlight lang="picat">
rms(Xs) = Y =>
Sum = sum_of_squares(Xs),
N = length(Xs),
Y = sqrt(Sum / N).
 
sum_of_squares(Xs) = Sum =>
Sum = 0,
foreach (X in Xs)
Sum := Sum + X * X
end.
 
main =>
Y = rms(1..10),
printf("The root-mean-square of 1..10 is %f\n", Y).
</syntaxhighlight>
{{out}}
<pre>
The root-mean-square of 1..10 is 6.204837
</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 5)
 
(let Lst (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0)
Line 729 ⟶ 1,607:
(length Lst) )
T )
*Scl ) ) )</langsyntaxhighlight>
{{out}}
Output:
<pre>6.20484</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i"> atest: Proc Options(main);
declare A(10) Dec Float(15) static initial (1,2,3,4,5,6,7,8,9,10);
declare (n,RMS) Dec Float(15);
n = hbound(A,1);
RMS = sqrt(sum(A**2)/n);
put Skip Data(rms);
End;</syntaxhighlight>
{{out}}
<pre>RMS= 6.20483682299543E+0000;</pre>
 
=={{header|PostScript}}==
<langsyntaxhighlight lang="postscript">/findrms{
/x exch def
/sum 0 def
Line 749 ⟶ 1,638:
}def
 
[1 2 3 4 5 6 7 8 9 10] findrms</langsyntaxhighlight>
{{out}}
Output:
<pre>
6.20483685
</pre>
{{libheader|initlib}}
<langsyntaxhighlight lang="postscript">[1 10] 1 range dup 0 {dup * +} fold exch length div sqrt</langsyntaxhighlight>
 
=={{header|Potion}}==
<syntaxhighlight lang="potion">rms = (series) :
total = 0.0
series each (x): total += x * x.
total /= series length
total sqrt
.
 
rms((1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) print</syntaxhighlight>
 
 
=={{header|Powerbuilder}}==
<langsyntaxhighlight lang="powerbuilder">long ll_x, ll_y, ll_product
decimal ld_rms
 
Line 769 ⟶ 1,669:
ld_rms = Sqrt(ll_product / ll_y)
 
//ld_rms value is 6.20483682299542849</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight PowerShelllang="powershell">function get-rms([float[]]$nums){
$sqsum=$nums | foreach-object { $_*$_} | measure-object -sum | select-object -expand Sum
return [math]::sqrt($sqsum/$nums.count)
}
 
get-rms @(1..10) </langsyntaxhighlight>
 
=={{header|Processing}}==
<syntaxhighlight lang="processing">void setup() {
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
print(rms(numbers));
}
 
float rms(float[] nums) {
float mean = 0;
for (float n : nums) {
mean += sq(n);
}
mean = sqrt(mean / nums.length);
return mean;
}</syntaxhighlight>
{{out}}
<pre>6.204837</pre>
 
=={{header|Prolog}}==
{{works with|GNU Prolog}}
<syntaxhighlight lang="prolog">
:- initialization(main).
 
rms(Xs, Y) :-
sum_of_squares(Xs, 0, Sum),
length(Xs, N),
Y is sqrt(Sum / N).
 
sum_of_squares([], Sum, Sum).
 
sum_of_squares([X|Xs], A, Sum) :-
A1 is A + X * X,
sum_of_squares(Xs, A1, Sum).
 
main :-
bagof(X, between(1, 10, X), Xs),
rms(Xs, Y),
format('The root-mean-square of 1..10 is ~f\n', [Y]).
</syntaxhighlight>
{{out}}
<pre>
The root-mean-square of 1..10 is 6.204837
</pre>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">NewList MyList() ; To hold a unknown amount of numbers to calculate
 
If OpenConsole()
Line 806 ⟶ 1,749:
PrintN("Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|3}}
<langsyntaxhighlight Pythonlang="python">>>> from math import sqrt
>>> def qmean(num):
return sqrt(sum(n*n for n in num)/len(num))
 
>>> qmean(range(1,11))
6.2048368229954285</langsyntaxhighlight>
<small>Note that function [http://docs.python.org/release/3.2/library/functions.html#range range] in Python includes the first limit of 1, excludes the second limit of 11, and has a default increment of 1.</small>
 
The Python 2 version of this is nearly identical, except you must cast the sum to a float to get float division instead of integer division; or better, do a <code>from __future__ import division</code>, which works on Python 2.2+ as well as Python 3, and makes division work consistently like it does in Python 3.
 
 
Alternatively in terms of '''reduce''':
<syntaxhighlight lang="python">from functools import (reduce)
from math import (sqrt)
 
 
# rootMeanSquare :: [Num] -> Float
def rootMeanSquare(xs):
return sqrt(reduce(lambda a, x: a + x * x, xs, 0) / len(xs))
 
 
print(
rootMeanSquare([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
)</syntaxhighlight>
{{Out}}
<pre>6.2048368229954285</pre>
 
=={{header|Qi}}==
<langsyntaxhighlight lang="qi">(define rms
R -> (sqrt (/ (APPLY + (MAPCAR * R R)) (length R))))</langsyntaxhighlight>
 
 
=={{header|Quackery}}==
 
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.
 
<syntaxhighlight lang="quackery">[ $ "bigrat.qky" loadfile ] now!
 
[ [] swap
witheach
[ unpack 2dup v*
join nested join ] ] is squareall ( [ --> [ )
 
[ dup size n->v rot
0 n->v rot
witheach
[ unpack v+ ]
2swap v/ ] is arithmean ( [ --> n/d )
[ dip
[ squareall arithmean ]
vsqrt drop ] is rms ( [ n --> n/d )
say "The RMS of the integers 1 to 10, to 80 decimal places with rounding." cr
say "(Checked on Wolfram Alpha. The final digit is correctly rounded up.)" cr cr
' [ [ 1 1 ] [ 2 1 ] [ 3 1 ] [ 4 1 ] [ 5 1 ]
[ 6 1 ] [ 7 1 ] [ 8 1 ] [ 9 1 ] [ 10 1 ] ]
( ^^^ the integers 1 to 10 represented as a nest of nested rational numbers )
80 rms
80 point$ echo$</syntaxhighlight>
 
{{out}}
 
<pre>The RMS of the integers 1 to 10, to 80 decimal places with rounding.
(Checked on Wolfram Alpha. The final digit is correctly rounded up.)
 
6.20483682299542829806662097772473784992796529536414069376132632095482141678247123</pre>
 
=={{header|R}}==
 
<lang R>sqrt(sum((1:10)^2/10))</lang>
The following function works for any vector x:
or generally, for x
<syntaxhighlight lang="rsplus">RMS <- function(x, na.rm = F) sqrt(mean(x^2, na.rm = na.rm))
<lang R>x<-1:10
 
sqrt(sum((x)^2/length(x)))</lang>
RMS(1:10)
# [1] 6.204837
 
RMS(c(NA, 1:10))
# [1] NA
 
RMS(c(NA, 1:10), na.rm = T)
# [1] 6.204837</syntaxhighlight>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
(define (rms nums)
(sqrt (/ (for/sum ([n nums]) (* n n)) (length nums))))
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" line>sub rms(*@nums) { sqrt ([+] @nums X** 2) / @nums }
 
say rms 1..10;</syntaxhighlight>
 
Here's a slightly more concise version, albeit arguably less readable:
<syntaxhighlight lang="raku" line>sub rms { sqrt @_ R/ [+] @_ X** 2 }</syntaxhighlight>
 
=={{header|REXX}}==
REXX has no built-in &nbsp; '''SQRTsqrt''' &nbsp; function, so a RYO version is included here.
<br><br>This particular SQRT&nbsp; '''sqrt''' &nbsp; function was programmed for speed, as this SQRT functionit has two critical components:
:::* &nbsp; the initial guess (for the square root)
:::* &nbsp; the number of (increasing) decimal digits used during the computations
<br>The SQRT&nbsp; '''sqrt''' &nbsp; code was optimized to use the minimum amount of digits (precision) for each iteration of the
<br>calculation as well as a reasonable attempt at providing a first-guess square root by essentially halving
<br>the number using logrithmiclogarithmic (base ten) arithmetic.
<langsyntaxhighlight lang="rexx">/*REXX program tocomputes computeand displays the root mean square (RMS) of a seriesnumber ofsequence. numbers.*/
parse arg nums digs show . /*obtain the optional arguments from CL*/
if nums=='' | nums=="," then nums=10 /*Not specified? Then use the default.*/
if digs=='' | digs=="," then digs=50 /* " " " " " " */
if show=='' | show=="," then show=10 /* " " " " " " */
numeric digits digs /*uses DIGS decimal digits for calc. */
$=0; do j=1 for nums /*process each of the N integers. */
$=$ + j**2 /*sum the squares of the integers. */
end /*j*/
/* [↓] displays SHOW decimal digits.*/
rms=format( sqrt($/nums), , show ) / 1 /*divide by N, then calculate the SQRT.*/
say 'root mean square for 1──►'nums "is: " rms /*display the root mean square (RMS). */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric digits; m.=9
numeric form; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
h=d+6; do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
return g</syntaxhighlight>
'''output''' &nbsp; when using the default inputs:
<pre>
root mean square for 1──►10 is: 6.204836823
</pre>
 
=={{header|Ring}}==
parse arg n . /*get the argument (maybe). */
<syntaxhighlight lang="ring">
if n=='' then n=10 /*Not specified? Then assume 10.*/
nums = [1,2,3,4,5,6,7,8,9,10]
numeric digits 50 /*let's go a little overboard. */
sum = 0
sum=0 /*sum of numbers squared (so far)*/
decimals(5)
do j=1 for n /*step through N integers. */
see "Average = " + average(nums) + nl
sum=sum+j**2 /*sum the squares of the integers*/
end /*j*/
rms=sqrt(sum/n) /*divide by N, then get SQRT. */
say 'root mean square for 1──►'n "is" rms /*show & tell.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────SQRT subroutine─────────────────────────*/
sqrt: procedure; parse arg x;if x=0 then return 0;d=digits();numeric digits 11
g=.sqrtGuess(); do j=0 while p>9; m.j=p; p=p%2+1; end; do k=j+5 to 0 by -1
if m.k>11 then numeric digits m.k;g=.5*(g+x/g);end;numeric digits d;return g/1
 
func average number
.sqrtGuess: if x<0 then say 'negative number' x; numeric form; m.=11
for i = 1 to len(number)
p=d+d%4+2; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; return g*.5'E'_%2</lang>
sum = sum + pow(number[i],2)
'''output'''
next
<pre style="overflow:scroll">
x = sqrt(sum / len(number))
root mean square for 1──►10 is 6.2048368229954282980666209777247378499279652953641
return x
</syntaxhighlight>
 
=={{header|RPL}}==
≪ LIST→ → n
≪ 0 1 n '''START''' SWAP SQ + '''NEXT'''
n / √
≫ ≫ '<span style="color:blue">RMS</span>' STO
 
{ 1 2 3 4 5 6 7 8 9 10 } <span style="color:blue">RMS</span>
{{out}}
<pre>
1: 6.204836823
</pre>
{{works with|HP|48G}}
≪ DUP ≪ SQ + ≫ STREAM SWAP SIZE / √
≫ '<span style="color:blue">RMS</span>' STO
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Array
def quadratic_mean
Math.sqrt( self.inject(0.0) {|s, y| s += y*y}.to_f / self.length )
end
end
Line 875 ⟶ 1,930:
end
 
(1..10).quadratic_mean # => 6.204836822995432048368229954285</langsyntaxhighlight>
 
and a non object-oriented solution:
<langsyntaxhighlight lang="ruby">def rms(seq)
Math.sqrt(seq.inject(0.0) sum{|sum, x| sum += x*x} / .fdiv(seq.lengthsize) )
end
puts rms (1..10).to_a # => 6.2048368229954285</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">valueList$ = "1 2 3 4 5 6 7 8 9 10"
while word$(valueList$,i +1) <> "" ' grab values from list
thisValue = val(word$(valueList$,i +1)) ' turn values into numbers
Line 891 ⟶ 1,946:
wend
print "List of Values:";valueList$;" containing ";i;" values"
print "Root Mean Square =";(sumSquares/i)^0.5</langsyntaxhighlight>
 
{{out}}
Output:
List of Values:1 2 3 4 5 6 7 8 9 10 containing 10 values
Root Mean Square =6.20483682
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn root_mean_square(vec: Vec<i32>) -> f32 {
let sum_squares = vec.iter().fold(0, |acc, &x| acc + x.pow(2));
return ((sum_squares as f32)/(vec.len() as f32)).sqrt();
}
 
fn main() {
let vec = (1..11).collect();
println!("The root mean square is: {}", root_mean_square(vec));
}</syntaxhighlight>
 
{{out}}
<pre>
The root mean square is: 6.204837
</pre>
 
=={{header|S-lang}}==
Many of math operations in S-Lang are 'vectorized', that is, given
an array, they apply themselves to each element. In this case, that
means no array_map() function needed. Also, "range arrays" have a
built-in syntax.
 
<syntaxhighlight lang="s-lang">define rms(arr)
{
return sqrt(sum(sqr(arr)) / length(arr));
}
 
print(rms([1:10]));</syntaxhighlight>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
-- irrms stands for Integer Ranged RMS
irrms(i, f:INT):FLT
Line 913 ⟶ 1,997:
#OUT + irrms(1, 10) + "\n";
end;
end;</langsyntaxhighlight>
 
=={{header|S-BASIC}}==
<syntaxhighlight lang="basic">
var n, sqsum, sqmean, rms = real
sqsum = 0
for n = 1 to 10 do
sqsum = sqsum + (n * n)
next n
sqmean = sqsum / n
rms = sqr(sqmean)
print "RMS of numbers from 1 to 10 = ";rms
 
end
</syntaxhighlight>
{{out}}
<pre>
RMS of numbers from 1 to 10 = 6.20484
</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def rms(nums: Seq[Int]) = math.sqrt(nums.map(math.pow(_, 2)).sum / nums.size)
println(rms(1 to 10))</langsyntaxhighlight>
{{out}}
<pre>6.2048368229954285</pre>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (rms nums)
(sqrt (/ (apply + (map * nums nums))
(length nums))))
 
(rms '(1 2 3 4 5 6 7 8 9 10))</langsyntaxhighlight>
{{out}}
Output:
<pre>6.20483682299543</pre>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 951 ⟶ 2,055:
begin
writeln(rms(numbers) digits 7);
end func;</langsyntaxhighlight>
 
=={{header|Shen}}==
{{works with|shen-scheme|0.17}}
<syntaxhighlight lang="shen">(declare scm.sqrt [number --> number])
 
(tc +)
 
(define mean
{ (list number) --> number }
Xs -> (/ (sum Xs) (length Xs)))
 
(define square
{ number --> number }
X -> (* X X))
 
(define rms
{ (list number) --> number }
Xs -> (scm.sqrt (mean (map (function square) Xs))))
 
(define iota-h
{ number --> number --> (list number) }
X X -> [X]
X Lim -> (cons X (iota-h (+ X 1) Lim)))
 
(define iota
{ number --> (list number) }
Lim -> (iota-h 1 Lim))
 
(output "~A~%" (rms (iota 10)))</syntaxhighlight>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func rms(a) {
sqrt(a.map{.**2}.sum / a.len)
}
 
say rms(1..10)</syntaxhighlight>
 
Using hyper operators, we can write it as:
<syntaxhighlight lang="ruby">func rms(a) { a »**» 2 «+» / a.len -> sqrt }</syntaxhighlight>
 
{{out}}
<pre>6.20483682299542829806662097772473784992796529536</pre>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">(((1 to: 10) inject: 0 into: [ :s :n | n*n + s ]) / 10) sqrt.</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
Line 960 ⟶ 2,106:
{{works with|CSnobol}}
There is no built-in sqrt( ) function in Snobol4+.
<langsyntaxhighlight SNOBOL4lang="snobol4"> define('rms(a)i,ssq') :(rms_end)
rms i = i + 1; ssq = ssq + (a<i> * a<i>) :s(rms)
rms = sqrt(1.0 * ssq / prototype(a)) :(return)
Line 969 ⟶ 2,115:
loop i = i + 1; str len(p) span('0123456789') . a<i> @p :s(loop)
output = str ' -> ' rms(a)
end</langsyntaxhighlight>
{{out}}
Output:
<pre>1 2 3 4 5 6 7 8 9 10 -> 6.20483682</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "calcrms" )
@( description, "Compute the Root mean square of the numbers 1..10." )
@( description, "The root mean square is also known by its initial RMS (or rms), and as the" )
@( description, "quadratic mean. The RMS is calculated as the mean of the squares of the" )
@( description, "numbers, square-rooted" )
@( see_also, "http://rosettacode.org/wiki/Averages/Root_mean_square" )
@( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure calcrms is
type float_arr is array(1..10) of float;
list : constant float_arr := (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0);
total: float := 0.0;
rms : float;
begin
for p in arrays.first(list)..arrays.last(list) loop
total := @ + list(p)**2;
end loop;
rms := numerics.sqrt( total / float(arrays.length(list)));
? rms;
end calcrms;</syntaxhighlight>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">fun rms(v: real vector) =
let
val v' = Vector.map (fn x => x*x) v
val sum = Vector.foldl op+ 0.0 v'
in
Math.sqrt( sum/real(Vector.length(v')) )
end;
 
rms(Vector.tabulate(10, fn n => real(n+1)));</syntaxhighlight>
{{out}}
<pre>val it = 6.204836823 : real</pre>
 
=={{header|Stata}}==
Compute the RMS of a variable and return the result in r(rms).
 
<syntaxhighlight lang="stata">program rms, rclass
syntax varname(numeric) [if] [in]
tempvar x
gen `x'=`varlist'^2 `if' `in'
qui sum `x' `if' `in'
return scalar rms=sqrt(r(mean))
end</syntaxhighlight>
 
'''Example'''
 
<syntaxhighlight lang="stata">clear
set obs 20
gen x=rnormal()
 
rms x
di r(rms)
1.0394189
 
rms x if x>0
di r(rms)
.7423647</syntaxhighlight>
 
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">extension Collection where Element: FloatingPoint {
@inlinable
public func rms() -> Element {
return (lazy.map({ $0 * $0 }).reduce(0, +) / Element(count)).squareRoot()
}
}
 
print("RMS of 1...10: \((1...10).map(Double.init).rms())")</syntaxhighlight>
 
{{out}}
 
<pre>RMS of 1...10: 6.2048368229954285</pre>
 
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">proc qmean list {
set sum 0.0
foreach value $list { set sum [expr {$sum + $value**2}] }
Line 981 ⟶ 2,207:
}
 
puts "RMS(1..10) = [qmean {1 2 3 4 5 6 7 8 9 10}]"</langsyntaxhighlight>
{{out}}
Output:
<pre>
RMS(1..10) = 6.2048368229954285
Line 989 ⟶ 2,215:
=={{header|Ursala}}==
using the <code>mean</code> function among others from the <code>flo</code> library
<langsyntaxhighlight Ursalalang="ursala">#import nat
#import flo
 
#cast %e
 
rms = sqrt mean sqr* float* nrange(1,10)</langsyntaxhighlight>
{{out}}
output:
<pre>
6.204837e+00
Line 1,002 ⟶ 2,228:
=={{header|Vala}}==
Valac probably needs to have the flag "-X -lm" added to include the C Math library.
<langsyntaxhighlight lang="vala">double rms(double[] list){
double sum_squares = 0;
double mean;
Line 1,020 ⟶ 2,246:
stdout.printf("%s\n", mean.to_string());
}</langsyntaxhighlight>
{{out}}
Output:
<pre>
6.2048368229954285
</pre>
 
=={{header|VBA}}==
Using Excel VBA
<syntaxhighlight lang="vb">Private Function root_mean_square(s() As Variant) As Double
For i = 1 To UBound(s)
s(i) = s(i) ^ 2
Next i
root_mean_square = Sqr(WorksheetFunction.sum(s) / UBound(s))
End Function
Public Sub pythagorean_means()
Dim s() As Variant
s = [{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]
Debug.Print root_mean_square(s)
End Sub</syntaxhighlight>
Without using Excel worksheetfunction:
<syntaxhighlight lang="vb">Function rms(iLow As Integer, iHigh As Integer)
Dim i As Integer
If iLow > iHigh Then
i = iLow
iLow = iHigh
iHigh = i
End If
For i = iLow To iHigh
rms = rms + i ^ 2
Next i
rms = Sqr(rms / (iHigh - iLow + 1))
End Function
 
Sub foo()
Debug.Print rms(1, 10)
End Sub
</syntaxhighlight>
 
Output:
<pre>
6.20483682299543
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import math
fn main() {
n := 10
mut sum := 0.0
for x := 1.0; x <= n; x++ {
sum += x * x
}
println(math.sqrt(sum / n))
}</syntaxhighlight>
 
{{out}}
<pre>
6.2048368229954
</pre>
 
=={{header|Wortel}}==
<syntaxhighlight lang="wortel">@let {
; using a composition and a fork (like you would do in J)
rms1 ^(@sqrt @(@sum / #) *^@sq)
 
; using a function with a named argument
rms2 &a @sqrt ~/ #a @sum !*^@sq a
 
[[
!rms1 @to 10
!rms2 @to 10
]]
}</syntaxhighlight>
{{out}}
<pre>[6.2048368229954285 6.2048368229954285]</pre>
 
=={{header|Wren}}==
<syntaxhighlight lang="wren">var rms = ((1..10).reduce(0) { |acc, i| acc + i*i }/10).sqrt
System.print(rms)</syntaxhighlight>
 
{{out}}
<pre>
6.2048368229954
</pre>
 
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">(defun quadratic-mean (xs)
(sqrt
(/
(apply +
(mapcar (lambda (x) (expt x 2)) xs))
(length xs))))
 
; define a RANGE function, for testing purposes
 
(defun range (x y)
(if (< x y)
(cons x (range (+ x 1) y))))
 
; test QUADRATIC-MEAN
 
(print (quadratic-mean (range 1 11)))</syntaxhighlight>
{{out}}
<pre>6.20483682299543</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code CrLf=9;
code real RlOut=48;
int N;
Line 1,035 ⟶ 2,360:
RlOut(0, sqrt(S/10.0));
CrLf(0);
]</langsyntaxhighlight>
{{out}}
Output:
<pre>
6.20484
</pre>
 
=={{header|Yacas}}==
<syntaxhighlight lang="yacas">Sqrt(Add((1 .. 10)^2)/10)</syntaxhighlight>
The above will give the precise solution <math>\sqrt{\frac{77}{2}}</math>, to downgrade to 6.20483682299, surround the expression with '<code>N()</code>'.
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn rms(z){ ( z.reduce(fcn(p,n){ p + n*n },0.0) /z.len() ).sqrt() }</syntaxhighlight>
The order in the reduce function is important as it coerces n*n to float.
<pre>
zkl: rms([1..10].walk()) //-->rms(T(1,2,3,4,5,6,7,8,9,10))
6.20484
</pre>
Anonymous user