Averages/Root mean square: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Python}}: make compatible with 2.x)
No edit summary
Line 8: Line 8:


C.f. [[Averages/Pythagorean means]]
C.f. [[Averages/Pythagorean means]]

=={{header|Lua}}==
<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})</lang>


=={{header|Python}}==
=={{header|Python}}==

Revision as of 09:52, 21 February 2010

Task
Averages/Root mean square
You are encouraged to solve this task according to the task description, using any language you may know.

Compute the Root mean square of the numbers 1..10.

The root mean square is also known by its initial RMS (or rms), and as the quadratic mean.

The RMS is calculated as the mean of the squares of the numbers, square-rooted:

C.f. Averages/Pythagorean means

Lua

<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})</lang>

Python

<lang Python>>>> from __future__ import division >>> from math import sqrt >>> def qmean(num): return sqrt(sum(n*n for n in num)/len(num))

>>> numbers = range(1,11) # 1..10 >>> qmean(numbers) 6.2048368229954285</lang>