Averages/Root mean square: Difference between revisions

Line 10:
 
=={{header|AutoHotkey}}==
'''Method 1 uses a loop to build the sum:'''
<lang autohotkey>MsgBox, % RMS(1, 10)
 
Line 20 ⟶ 21:
Sum += (a + A_Index - 1) ** 2
Return, Sqrt(Sum / n)
}</lang>
Message box shows:
<pre>
6.204837
</pre>
'''Method 2 does not need a loop:'''<br>
Using these equations:<br>
<math>\sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6}</math> See [[wp:List of mathematical series]]<br><br>
for <math>a<b</math> : <math>\sum_{i=a}^b i^2 = \sum_{i=1}^b i^2 - \sum_{i=1}^{a-1} i^2</math><br><br>
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>
<lang autohotkey>MsgBox, % RMS(1, 10)
 
 
;---------------------------------------------------------------------------
RMS(a, b) { ; Root Mean Square of integers a through b
;---------------------------------------------------------------------------
Return, Sqrt((b*(b+1)*(2*b+1)-a*(a-1)*(2*a-1))/6/(b-a+1))
}</lang>
Message box shows:
138

edits