Add all the A tasks
This commit is contained in:
parent
2dd7375f96
commit
051504d65b
1608 changed files with 18584 additions and 0 deletions
8
Task/Averages-Root-mean-square/0DESCRIPTION
Normal file
8
Task/Averages-Root-mean-square/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Compute the [[wp:Root mean square|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:
|
||||
: <math>x_{\mathrm{rms}} = \sqrt {{{x_1}^2 + {x_2}^2 + \cdots + {x_n}^2} \over n}. </math>
|
||||
|
||||
Cf. [[Averages/Pythagorean means]]
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
# Define the rms PROCedure & ABS OPerators for LONG... REAL #
|
||||
MODE RMSFIELD = #LONG...# REAL;
|
||||
PROC (RMSFIELD)RMSFIELD rms field sqrt = #long...# sqrt;
|
||||
INT rms field width = #long...# real width;
|
||||
|
||||
PROC crude rms = ([]RMSFIELD v)RMSFIELD: (
|
||||
RMSFIELD sum := 0;
|
||||
FOR i FROM LWB v TO UPB v DO sum +:= v[i]**2 OD;
|
||||
rms field sqrt(sum / (UPB v - LWB v + 1))
|
||||
);
|
||||
|
||||
PROC rms = ([]RMSFIELD v)RMSFIELD: (
|
||||
# round off error accumulated at standard precision #
|
||||
RMSFIELD sum := 0, round off error:= 0;
|
||||
FOR i FROM LWB v TO UPB v DO
|
||||
RMSFIELD org = sum, prod = v[i]**2;
|
||||
sum +:= prod;
|
||||
round off error +:= sum - org - prod
|
||||
OD;
|
||||
rms field sqrt((sum - round off error)/(UPB v - LWB v + 1))
|
||||
);
|
||||
|
||||
main: (
|
||||
[]RMSFIELD one to ten = (1,2,3,4,5,6,7,8,9,10);
|
||||
|
||||
print(("crude rms(one to ten): ", crude rms(one to ten), new line));
|
||||
print(("rms(one to ten): ", rms(one to ten), new line))
|
||||
)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
rms←{((+/⍵*2)÷⍴⍵)*0.5}
|
||||
x←⍳10
|
||||
|
||||
rms x
|
||||
6.204836823
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#!/usr/bin/awk -f
|
||||
# computes RMS of the 1st column of a data file
|
||||
{
|
||||
x = $1; # value of 1st column
|
||||
S += x*x;
|
||||
N++;
|
||||
}
|
||||
|
||||
END {
|
||||
print "RMS: ",sqrt(S/N);
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
|
||||
with Ada.Numerics.Elementary_Functions;
|
||||
use Ada.Numerics.Elementary_Functions;
|
||||
procedure calcrms is
|
||||
type float_arr is array(1..10) of Float;
|
||||
|
||||
function rms(nums : float_arr) return Float is
|
||||
sum : Float := 0.0;
|
||||
begin
|
||||
for p in nums'Range loop
|
||||
sum := sum + nums(p)**2;
|
||||
end loop;
|
||||
return sqrt(sum/Float(nums'Length));
|
||||
end rms;
|
||||
|
||||
list : float_arr;
|
||||
begin
|
||||
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;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
MsgBox, % RMS(1, 10)
|
||||
|
||||
|
||||
;---------------------------------------------------------------------------
|
||||
RMS(a, b) { ; Root Mean Square of integers a through b
|
||||
;---------------------------------------------------------------------------
|
||||
n := b - a + 1
|
||||
Loop, %n%
|
||||
Sum += (a + A_Index - 1) ** 2
|
||||
Return, Sqrt(Sum / n)
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
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))
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
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
|
||||
18
Task/Averages-Root-mean-square/C/averages-root-mean-square.c
Normal file
18
Task/Averages-Root-mean-square/C/averages-root-mean-square.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
double rms(double *v, int n)
|
||||
{
|
||||
int i;
|
||||
double sum = 0.0;
|
||||
for(i = 0; i < n; i++)
|
||||
sum += v[i] * v[i];
|
||||
return sqrt(sum / n);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
double v[] = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.};
|
||||
printf("%f\n", rms(v, sizeof(v)/sizeof(double)));
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(use '[clojure.contrib.math :only (sqrt)])
|
||||
|
||||
(defn rms [xs]
|
||||
(sqrt (/ (reduce + (map #(* % %) xs))
|
||||
(count xs))))
|
||||
|
||||
(println (rms (range 1 11)))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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])
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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]).
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
: rms ( faddr len -- frms )
|
||||
dup >r 0e
|
||||
floats bounds do
|
||||
i f@ fdup f* f+
|
||||
float +loop
|
||||
r> s>f f/ fsqrt ;
|
||||
|
||||
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
|
||||
|
|
@ -0,0 +1 @@
|
|||
print *,sqrt( sum(x**2)/size(x) )
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
const n = 10
|
||||
sum := 0.
|
||||
for x := 1.; x <= n; x++ {
|
||||
sum += x * x
|
||||
}
|
||||
fmt.Println(math.Sqrt(sum / n))
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
main = print $ mean 2 [1 .. 10]
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
public class RMS {
|
||||
public static double rms(double[] nums){
|
||||
double ms = 0;
|
||||
for (int i = 0; i < nums.length; i++)
|
||||
ms += nums[i] * nums[i];
|
||||
ms /= nums.length;
|
||||
return Math.sqrt(ms);
|
||||
}
|
||||
|
||||
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 " + rms(nums));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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})
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
use v5.10.0;
|
||||
sub rms
|
||||
{
|
||||
my $r = 0;
|
||||
$r += $_**2 for @_;
|
||||
return sqrt( $r/@_ );
|
||||
}
|
||||
|
||||
say rms(1..10);
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
(scl 5)
|
||||
|
||||
(let Lst (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0)
|
||||
(prinl
|
||||
(format
|
||||
(sqrt
|
||||
(*/
|
||||
(sum '((N) (*/ N N 1.0)) Lst)
|
||||
1.0
|
||||
(length Lst) )
|
||||
T )
|
||||
*Scl ) ) )
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
>>> from math import sqrt
|
||||
>>> def qmean(num):
|
||||
return sqrt(sum(n*n for n in num)/len(num))
|
||||
|
||||
>>> qmean(range(1,11))
|
||||
6.2048368229954285
|
||||
|
|
@ -0,0 +1 @@
|
|||
sqrt(sum((1:10)^2/10))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
x<-1:10
|
||||
sqrt(sum((x)^2/length(x)))
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
/*REXX program to compute the root mean square of a series of numbers.*/
|
||||
|
||||
parse arg n . /*get the argument (maybe). */
|
||||
if n=='' then n=10 /*Not specified? Then assume 10.*/
|
||||
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 /*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
|
||||
|
||||
.sqrtGuess: if x<0 then say 'negative number' x; numeric form; m.=11
|
||||
p=d+d%4+2; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; return g*.5'E'_%2
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
class Array
|
||||
def quadratic_mean
|
||||
Math.sqrt( self.inject(0) {|s, y| s += y*y}.to_f / self.length )
|
||||
end
|
||||
end
|
||||
|
||||
class Range
|
||||
def quadratic_mean
|
||||
self.to_a.quadratic_mean
|
||||
end
|
||||
end
|
||||
|
||||
(1..10).quadratic_mean # => 6.20483682299543
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
def rms(seq)
|
||||
Math.sqrt(seq.inject(0.0) {|sum, x| sum += x*x} / seq.length)
|
||||
end
|
||||
puts rms (1..10).to_a # => 6.2048368229954285
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
class MAIN is
|
||||
-- irrms stands for Integer Ranged RMS
|
||||
irrms(i, f:INT):FLT
|
||||
pre i <= f
|
||||
is
|
||||
sum ::= 0;
|
||||
loop
|
||||
sum := sum + i.upto!(f).pow(2);
|
||||
end;
|
||||
return (sum.flt / (f-i+1).flt).sqrt;
|
||||
end;
|
||||
|
||||
main is
|
||||
#OUT + irrms(1, 10) + "\n";
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def rms(nums: Seq[Int]) = math.sqrt(nums.map(math.pow(_, 2)).sum / nums.size)
|
||||
println(rms(1 to 10))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(define (rms nums)
|
||||
(sqrt (/ (apply + (map * nums nums))
|
||||
(length nums))))
|
||||
|
||||
(rms '(1 2 3 4 5 6 7 8 9 10))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(((1 to: 10) inject: 0 into: [ :s :n | n*n + s ]) / 10) sqrt.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
proc qmean list {
|
||||
set sum 0.0
|
||||
foreach value $list { set sum [expr {$sum + $value**2}] }
|
||||
return [expr { sqrt($sum / [llength $list]) }]
|
||||
}
|
||||
|
||||
puts "RMS(1..10) = [qmean {1 2 3 4 5 6 7 8 9 10}]"
|
||||
Loading…
Add table
Add a link
Reference in a new issue