Averages/Pythagorean means: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 1:
[[Category:Geometry]]
{{task}}
 
;Task
{{task heading}}
 
Compute all three of the [[wp:Pythagorean means|Pythagorean means]] of the set of integers <big>1</big> through <big>10</big> (inclusive).
Line 25 ⟶ 26:
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F amean(num)
R sum(num)/Float(num.len)
Line 48 ⟶ 49:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang=Action"action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
PROC InverseI(INT a,result)
Line 126 ⟶ 127:
 
=={{header|ActionScript}}==
<syntaxhighlight lang=ActionScript"actionscript">function arithmeticMean(v:Vector.<Number>):Number
{
var sum:Number = 0;
Line 155 ⟶ 156:
 
pythagorean_means.ads:
<syntaxhighlight lang=Ada"ada">package Pythagorean_Means is
type Set is array (Positive range <>) of Float;
function Arithmetic_Mean (Data : Set) return Float;
Line 163 ⟶ 164:
 
pythagorean_means.adb:
<syntaxhighlight lang=Ada"ada">with Ada.Numerics.Generic_Elementary_Functions;
package body Pythagorean_Means is
package Math is new Ada.Numerics.Generic_Elementary_Functions (Float);
Line 198 ⟶ 199:
 
example main.adb:
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO;
with Pythagorean_Means;
procedure Main is
Line 217 ⟶ 218:
{{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]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - argc and argv implementation dependent}}
<syntaxhighlight lang="algol68">main: (
INT count:=0;
LONG REAL f, sum:=0, prod:=1, resum:=0;
Line 257 ⟶ 258:
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
% returns the arithmetic mean of the elements of n from lo to hi %
real procedure arithmeticMean ( real array n ( * ); integer value lo, hi ) ;
Line 302 ⟶ 303:
=={{header|Amazing Hopper}}==
Think about "talk" programming...
<syntaxhighlight lang=Amazing"amazing Hopperhopper">
#include <hopper.h>
 
Line 398 ⟶ 399:
 
=={{header|APL}}==
<syntaxhighlight lang=APL"apl">
arithmetic←{(+/⍵)÷⍴⍵}
geometric←{(×/⍵)*÷⍴⍵}
Line 415 ⟶ 416:
=={{header|AppleScript}}==
{{trans|JavaScript}}
<syntaxhighlight lang=AppleScript"applescript">-- arithmetic_mean :: [Number] -> Number
on arithmetic_mean(xs)
Line 517 ⟶ 518:
end mReturn</syntaxhighlight>
{{Out}}
<syntaxhighlight lang=AppleScript"applescript">{values:{arithmetic:5.5, geometric:4.528728688117, harmonic:3.414171521474},
inequalities:{|A >= G|:true}, |G >= H|:true}</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">arithmeticMean: function [arr]->
average arr
 
Line 542 ⟶ 543:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">A := ArithmeticMean(1, 10)
G := GeometricMean(1, 10)
H := HarmonicMean(1, 10)
Line 592 ⟶ 593:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">#!/usr/bin/awk -f
{
x = $1; # value of 1st column
Line 609 ⟶ 610:
=={{header|BBC BASIC}}==
The arithmetic and harmonic means use BBC BASIC's built-in array operations; only the geometric mean needs a loop.
<syntaxhighlight lang="bbcbasic"> DIM a(9)
a() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
PRINT "Arithmetic mean = " ; FNarithmeticmean(a())
Line 639 ⟶ 640:
 
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h> // atoi()
#include <math.h> // pow()
Line 668 ⟶ 669:
{{works with|C sharp|C#|3}}
 
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Diagnostics;
Line 709 ⟶ 710:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <vector>
#include <iostream>
#include <numeric>
Line 738 ⟶ 739:
 
=={{header|Clojure}}==
<syntaxhighlight lang=Clojure"clojure">(use '[clojure.contrib.math :only (expt)])
(defn a-mean [coll]
Line 755 ⟶ 756:
 
=={{header|CoffeeScript}}==
<syntaxhighlight lang="coffeescript">a = [ 1..10 ]
arithmetic_mean = (a) -> a.reduce(((s, x) -> s + x), 0) / a.length
geometic_mean = (a) -> Math.pow(a.reduce(((s, x) -> s * x), 1), (1 / a.length))
Line 771 ⟶ 772:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">(defun generic-mean (nums reduce-op final-op)
(funcall final-op (reduce reduce-op nums)))
 
Line 797 ⟶ 798:
=={{header|D}}==
The output for the harmonic mean is wrong.
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.functional;
 
auto aMean(T)(T data) pure nothrow @nogc {
Line 820 ⟶ 821:
 
=={{header|Delphi}}==
<syntaxhighlight lang=Delphi"delphi">program AveragesPythagoreanMeans;
 
{$APPTYPE CONSOLE}
Line 875 ⟶ 876:
Given that we're defining all three together, it makes sense to express their regularities:
 
<syntaxhighlight lang="e">def makeMean(base, include, finish) {
return def mean(numbers) {
var count := 0
Line 891 ⟶ 892:
def H := makeMean(0, fn b,x { b+1/x }, fn acc,n { n / acc })</syntaxhighlight>
 
<syntaxhighlight lang="e">? A(1..10)
# value: 5.5
 
Line 901 ⟶ 902:
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
(define (A xs) (// (for/sum ((x xs)) x) (length xs)))
 
Line 914 ⟶ 915:
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Means do
def arithmetic(list) do
Enum.sum(list) / length(list)
Line 941 ⟶ 942:
=={{header|Erlang}}==
 
<syntaxhighlight lang=Erlang"erlang">%% Author: Abhay Jain <abhay_1303@yahoo.co.in>
 
-module(mean_calculator).
Line 983 ⟶ 984:
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM MEANS
 
Line 1,031 ⟶ 1,032:
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang=Euler"euler Mathmath Toolboxtoolbox">
>function A(x) := mean(x)
>function G(x) := exp(mean(log(x)))
Line 1,043 ⟶ 1,044:
Alternatively, e.g.,
 
<syntaxhighlight lang=Euler"euler Mathmath Toolboxtoolbox">
>function G(x) := prod(x)^(1/length(x))
</syntaxhighlight>
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">function arithmetic_mean(sequence s)
atom sum
if length(s) = 0 then
Line 1,112 ⟶ 1,113:
Use the functions : AVERAGE, GEOMEAN and HARMEAN
 
<syntaxhighlight lang=Excel"excel">
=AVERAGE(1;2;3;4;5;6;7;8;9;10)
=GEOMEAN(1;2;3;4;5;6;7;8;9;10)
Line 1,125 ⟶ 1,126:
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">let P = [1.0; 2.0; 3.0; 4.0; 5.0; 6.0; 7.0; 8.0; 9.0; 10.0]
 
let arithmeticMean (x : float list) =
Line 1,145 ⟶ 1,146:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">: a-mean ( seq -- mean )
[ sum ] [ length ] bi / ;
 
Line 1,160 ⟶ 1,161:
=={{header|Fantom}}==
 
<syntaxhighlight lang="fantom">
class Main
{
Line 1,205 ⟶ 1,206:
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: famean ( faddr n -- f )
0e
tuck floats bounds do
Line 1,235 ⟶ 1,236:
=={{header|Fortran}}==
{{works with|Fortran|90}}
<syntaxhighlight lang="fortran">program Mean
 
real :: a(10) = (/ (i, i=1,10) /)
Line 1,253 ⟶ 1,254:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
' FB 1.05.0 Win64
 
Line 1,304 ⟶ 1,305:
 
=={{header|FunL}}==
<syntaxhighlight lang="funl">import lists.zip
 
def
Line 1,332 ⟶ 1,333:
=={{header|Futhark}}==
 
<syntaxhighlight lang=Futhark"futhark">
fun arithmetic_mean(as: [n]f64): f64 =
reduce (+) 0.0 (map (/f64(n)) as)
Line 1,349 ⟶ 1,350:
 
=={{header|GAP}}==
<syntaxhighlight lang="gap"># The first two work with rationals or with floats
# (but bear in mind that support of floating point is very poor in GAP)
mean := v -> Sum(v) / Length(v);
Line 1,369 ⟶ 1,370:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 1,395 ⟶ 1,396:
=={{header|Groovy}}==
Solution:
<syntaxhighlight lang="groovy">def arithMean = { list ->
list == null \
? null \
Line 1,420 ⟶ 1,421:
 
Test:
<syntaxhighlight lang="groovy">def list = 1..10
def A = arithMean(list)
def G = geomMean(list)
Line 1,443 ⟶ 1,444:
The [[wp:Generalized mean|general function]] given here yields an arithmetic mean when its first argument is <code>1</code>, a geometric mean when its first argument is <code>0</code>, and a harmonic mean when its first argument is <code>-1</code>.
 
<syntaxhighlight lang="haskell">import Data.List (genericLength)
import Control.Monad (zipWithM_)
 
Line 1,458 ⟶ 1,459:
These three functions (each combining the length of a list with some kind of fold over the elements of that same list), all share the same applicative structure.
 
<syntaxhighlight lang="haskell">import Data.List (genericLength)
 
-- ARITHMETIC, GEOMETRIC AND HARMONIC MEANS ---------------
Line 1,485 ⟶ 1,486:
 
=={{header|HicEst}}==
<syntaxhighlight lang=HicEst"hicest">AGH = ALIAS( A, G, H ) ! named vector elements
AGH = (0, 1, 0)
DO i = 1, 10
Line 1,498 ⟶ 1,499:
 
=={{header|Icon}} and {{header|Unicon}}==
<syntaxhighlight lang=Icon"icon">link numbers # for a/g/h means
 
procedure main()
Line 1,513 ⟶ 1,514:
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers:amean, numbers:gmean, and numbers:hmean] are shown below:
<syntaxhighlight lang=Icon"icon">procedure amean(L[]) #: arithmetic mean
local m
if *L = 0 then fail
Line 1,553 ⟶ 1,554:
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang=IS"is-BASICbasic">100 PROGRAM "Averages.bas"
110 NUMERIC ARR(1 TO 10)
120 FOR I=LBOUND(ARR) TO UBOUND(ARR)
Line 1,585 ⟶ 1,586:
=={{header|J}}==
'''Solution:'''
<syntaxhighlight lang="j">amean=: +/ % #
gmean=: # %: */
hmean=: amean&.:%</syntaxhighlight>
 
'''Example Usage:'''
<syntaxhighlight lang="j"> (amean , gmean , hmean) >: i. 10
5.5 4.528729 3.414172
assert 2 >:/\ (amean , gmean , hmean) >: i. 10 NB. check amean >= gmean and gmean >= hmean</syntaxhighlight>
Line 1,596 ⟶ 1,597:
Note that gmean could have instead been defined as mean under logarithm, for example:
 
<syntaxhighlight lang="j">gmean=:amean&.:^.</syntaxhighlight>
 
(and this variant should probably be preferred - especially if the argument list is long, to avoid problems with floating point infinity.)
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.util.Arrays;
import java.util.List;
 
Line 1,648 ⟶ 1,649:
{{works with|Java|1.8}}
We can rewrite the 3 methods using the new JAVA Stream API:
<syntaxhighlight lang="java">
public static double arithmAverage(double array[]){
if (array == null ||array.length == 0) {
Line 1,690 ⟶ 1,691:
 
===ES5===
<syntaxhighlight lang="javascript">(function () {
'use strict';
 
Line 1,753 ⟶ 1,754:
 
{{Out}}
<syntaxhighlight lang=JavaScript"javascript">{
"values": {
"Arithmetic": 5.5,
Line 1,763 ⟶ 1,764:
 
===ES6===
<syntaxhighlight lang=JavaScript"javascript">(() => {
 
// arithmeticMean :: [Number] -> Number
Line 1,827 ⟶ 1,828:
})();</syntaxhighlight>
{{Out}}
<syntaxhighlight lang=JavaScript"javascript">{
"values": {
"Arithmetic": 5.5,
Line 1,837 ⟶ 1,838:
 
=={{header|jq}}==
<syntaxhighlight lang="jq">def amean: add/length;
 
def logProduct: map(log) | add;
Line 1,859 ⟶ 1,860:
Julia has a `mean` function to compute the arithmetic mean of a collections
of numbers. We can redefine it as follows.
<syntaxhighlight lang=Julia"julia">amean(A) = sum(A)/length(A)
 
gmean(A) = prod(A)^(1/length(A))
Line 1,874 ⟶ 1,875:
 
=={{header|K}}==
<syntaxhighlight lang=K"k">
am:{(+/x)%#x}
gm:{(*/x)^(%#x)}
Line 1,884 ⟶ 1,885:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">import kotlin.math.round
import kotlin.math.pow
 
Line 1,912 ⟶ 1,913:
 
=={{header|Lasso}}==
<syntaxhighlight lang=Lasso"lasso">define arithmetic_mean(a::staticarray)::decimal => {
//sum of the list divided by its length
return (with e in #a sum #e) / decimal(#a->size)
Line 1,937 ⟶ 1,938:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">for i = 1 to 10
a = a + i
next
Line 1,965 ⟶ 1,966:
 
=={{header|Logo}}==
<syntaxhighlight lang="logo">to compute_means :count
local "sum
make "sum 0
Line 1,990 ⟶ 1,991:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">function fsum(f, a, ...) return a and f(a) + fsum(f, ...) or 0 end
function pymean(t, f, finv) return finv(fsum(f, unpack(t)) / #t) end
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Line 2,011 ⟶ 2,012:
 
 
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
Module CheckIt {
sum=lambda -> {
Line 2,067 ⟶ 2,068:
 
=={{header|Maple}}==
<syntaxhighlight lang=Maple"maple">x := [ seq( 1 .. 10 ) ];
Means := proc( x )
uses Statistics;
Line 2,084 ⟶ 2,085:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">Print["{Arithmetic Mean, Geometric Mean, Harmonic Mean} = ",
N@Through[{Mean, GeometricMean, HarmonicMean}[Range@10]]]</syntaxhighlight>
{{out}}
Line 2,090 ⟶ 2,091:
 
=={{header|MATLAB}}==
<syntaxhighlight lang=MATLAB"matlab">function [A,G,H] = pythagoreanMeans(list)
A = mean(list);
Line 2,100 ⟶ 2,101:
A solution that works for both, Matlab and Octave, is this
 
<syntaxhighlight lang=MATLAB"matlab">function [A,G,H] = pythagoreanMeans(list)
A = mean(list); % arithmetic mean
G = exp(mean(log(list))); % geometric mean
Line 2,107 ⟶ 2,108:
 
Solution:
<syntaxhighlight lang=MATLAB"matlab">>> [A,G,H]=pythagoreanMeans((1:10))
 
A =
Line 2,124 ⟶ 2,125:
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">/* built-in */
L: makelist(i, i, 1, 10)$
 
Line 2,132 ⟶ 2,133:
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE PythagoreanMeans;
FROM FormatString IMPORT FormatString;
FROM LongMath IMPORT power;
Line 2,214 ⟶ 2,215:
=={{header|MUMPS}}==
 
<syntaxhighlight lang=MUMPS"mumps">Pyth(n) New a,ii,g,h,x
For ii=1:1:n set x(ii)=ii
;
Line 2,239 ⟶ 2,240:
=={{header|NetRexx}}==
{{trans|ooRexx}}
<syntaxhighlight lang=NetRexx"netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,294 ⟶ 2,295:
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import math, sequtils, sugar
proc amean(num: seq[float]): float =
Line 2,324 ⟶ 2,325:
=={{header|Oberon-2}}==
Oxford Oberon-2
<syntaxhighlight lang="oberon2">
MODULE PythMean;
IMPORT Out, ML := MathL;
Line 2,370 ⟶ 2,371:
=={{header|Objeck}}==
{{trans|Java}}
<syntaxhighlight lang="objeck">class PythagMeans {
function : Main(args : String[]) ~ Nil {
array := [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
Line 2,428 ⟶ 2,429:
The three means in one function
 
<syntaxhighlight lang="ocaml">let means v =
let n = Array.length v
and a = ref 0.0
Line 2,448 ⟶ 2,449:
Another implementation using <code>[http://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html#VALfold_left Array.fold_left]</code> instead of a '''for''' loop:
 
<syntaxhighlight lang="ocaml">let means v =
let (a, b, c) =
Array.fold_left
Line 2,459 ⟶ 2,460:
 
=={{header|Octave}}==
<syntaxhighlight lang=Octave"octave">
A = mean(list); % arithmetic mean
G = mean(list,'g'); % geometric mean
Line 2,469 ⟶ 2,470:
=={{header|Oforth}}==
 
<syntaxhighlight lang=Oforth"oforth">import: mapping
 
: A ( x )
Line 2,495 ⟶ 2,496:
 
=={{header|ooRexx}}==
<syntaxhighlight lang=ooRexx"oorexx">a = .array~of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
say "Arithmetic =" arithmeticMean(a)", Geometric =" geometricMean(a)", Harmonic =" harmonicMean(a)
 
Line 2,540 ⟶ 2,541:
 
=={{header|Oz}}==
<syntaxhighlight lang="oz">declare
%% helpers
fun {Sum Xs} {FoldL Xs Number.'+' 0.0} end
Line 2,574 ⟶ 2,575:
=={{header|PARI/GP}}==
General implementations:
<syntaxhighlight lang="parigp">arithmetic(v)={
sum(i=1,#v,v[i])/#v
};
Line 2,588 ⟶ 2,589:
 
Specific to the first ''n'' positive integers:
<syntaxhighlight lang="parigp">arithmetic_first(n)={
(n+1)/2
};
Line 2,611 ⟶ 2,612:
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">sub A
{
my $a = 0;
Line 2,639 ⟶ 2,640:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">arithmetic_mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 2,671 ⟶ 2,672:
 
=={{header|PHP}}==
<syntaxhighlight lang=PHP"php"><?php
// Created with PHP 7.0
 
Line 2,709 ⟶ 2,710:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(load "@lib/math.l")
 
(let (Lst (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0) Len (length Lst))
Line 2,730 ⟶ 2,731:
 
=={{header|PL/I}}==
<syntaxhighlight lang=PL"pl/Ii">
declare n fixed binary,
(Average, Geometric, Harmonic) float;
Line 2,761 ⟶ 2,762:
 
=={{header|PostScript}}==
<syntaxhighlight lang="text">
/pythamean{
/x exch def
Line 2,794 ⟶ 2,795:
 
{{libheader|initlib}}
<syntaxhighlight lang="postscript">
/numbers {[1 10] 1 range}.
/recip {1 exch div}.
Line 2,807 ⟶ 2,808:
 
=={{header|PowerShell}}==
<syntaxhighlight lang=PowerShell"powershell">$A = 0
$LogG = 0
$InvH = 0
Line 2,839 ⟶ 2,840:
 
=={{header|Processing}}==
<syntaxhighlight lang=Processing"processing">void setup() {
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
println("Arithmetic mean: " + arithmeticMean(numbers));
Line 2,878 ⟶ 2,879:
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">Procedure.d ArithmeticMean()
For a = 1 To 10
mean + a
Line 2,907 ⟶ 2,908:
=={{header|Python}}==
{{works with|Python|3}}
<syntaxhighlight lang=Python"python">from operator import mul
from functools import reduce
 
Line 2,936 ⟶ 2,937:
Uses <code>root</code> from [[Integer roots#Quackery]].
 
<syntaxhighlight lang=Quackery"quackery"> [] 10 times [ i^ 1+ join ]
 
say "Arithmetic mean:" sp
Line 2,962 ⟶ 2,963:
=={{header|R}}==
Initialise x
<syntaxhighlight lang=R"r">
x <- 1:10
</syntaxhighlight>
Arithmetic mean
<syntaxhighlight lang=R"r">
a <- sum(x)/length(x)
 
</syntaxhighlight>
or
<syntaxhighlight lang=R"r">
a <- mean(x)
</syntaxhighlight>
 
The geometric mean
<syntaxhighlight lang=R"r">
g <- prod(x)^(1/length(x))
</syntaxhighlight>
 
The harmonic mean (no error checking that <math>x_i\ne 0,\text{ } \forall \text{ }i=1\ldots n</math>)
<syntaxhighlight lang=R"r">
h <- length(x)/sum(1/x)
</syntaxhighlight>
Line 2,987 ⟶ 2,988:
Then:
 
<syntaxhighlight lang=R"r">
a > g
</syntaxhighlight>
Line 2,993 ⟶ 2,994:
and
 
<syntaxhighlight lang=R"r">
g > h
</syntaxhighlight>
Line 3,002 ⟶ 3,003:
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
Line 3,035 ⟶ 3,036:
{{works with|Rakudo|2015.12}}
 
<syntaxhighlight lang=perl6"raku" line>sub A { ([+] @_) / @_ }
sub G { ([*] @_) ** (1 / @_) }
sub H { @_ / [+] 1 X/ @_ }
Line 3,052 ⟶ 3,053:
REXX doesn't have a &nbsp; '''POW''' &nbsp; function, so an &nbsp; '''IROOT''' &nbsp; ('''i'''nteger '''root''') &nbsp; function is included here; &nbsp; it includes an
<br>extra error check if used as a general purpose function that would otherwise yield a complex result.
<syntaxhighlight lang="rexx">/*REXX program computes and displays the Pythagorean means [Amean, Gmean, Hmean]. */
numeric digits 20 /*use a little extra for the precision.*/
parse arg n . /*obtain the optional argument from CL.*/
Line 3,098 ⟶ 3,099:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
decimals(8)
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Line 3,138 ⟶ 3,139:
=={{header|Ruby}}==
{{works with|Ruby|1.9+}}
<syntaxhighlight lang="ruby">class Array
def arithmetic_mean
inject(0.0, :+) / length
Line 3,176 ⟶ 3,177:
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">bXsum = 1
for i = 1 to 10
sum = sum + i ' sum of 1 -> 10
Line 3,200 ⟶ 3,201:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn main() {
let mut sum = 0.0;
let mut prod = 1;
Line 3,224 ⟶ 3,225:
=={{header|Scala}}==
{{works with|Scala|2.8+}}
<syntaxhighlight lang="scala">def arithmeticMean(n: Seq[Int]) = n.sum / n.size.toDouble
def geometricMean(n: Seq[Int]) = math.pow(n.foldLeft(1.0)(_*_), 1.0 / n.size.toDouble)
def harmonicMean(n: Seq[Int]) = n.size / n.map(1.0 / _).sum
Line 3,245 ⟶ 3,246:
=={{header|Scheme}}==
{{Works with|Scheme|R<math>^5</math>RS}}
<syntaxhighlight lang="scheme">(define (a-mean l)
(/ (apply + l) (length l)))
 
Line 3,269 ⟶ 3,270:
(newline))</syntaxhighlight>
{{out}}
<syntaxhighlight lang="text">11/2 >= 4.528728688116765 >= 25200/7381
#t</syntaxhighlight>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 3,303 ⟶ 3,304:
 
=={{header|Sidef}}==
<syntaxhighlight lang="sidef">func A(a) { a.sum / a.len }
func G(a) { a.prod.root(a.len) }
func H(a) { a.len / a.map{1/_}.sum }</syntaxhighlight>
 
The same thing, using hyper-operators:
<syntaxhighlight lang="sidef">func A(a) { a«+» / a.len }
func G(a) { a«*» ** (1/a.len) }
func H(a) { a.len / (a«/«1 «+») }</syntaxhighlight>
 
Calling the functions:
<syntaxhighlight lang="sidef">say("A(1,...,10) = ", A(1..10));
say("G(1,...,10) = ", G(1..10));
say("H(1,...,10) = ", H(1..10));</syntaxhighlight>
Line 3,326 ⟶ 3,327:
This extends the class Collection, so these three methods can be called over any kind of collection, it is enough the the objects of the collection understand +, *, raisedTo, reciprocal and /.
 
<syntaxhighlight lang="smalltalk">Collection extend
[
arithmeticMean
Line 3,363 ⟶ 3,364:
=={{header|SQL}}==
It may not be possible to calculate a geometric mean in a query, but the other two are easy enough.
<syntaxhighlight lang="sql">
--setup
create table averages (val integer);
Line 3,392 ⟶ 3,393:
=={{header|Stata}}==
The command [http://www.stata.com/help.cgi?ameans ameans] prints the arithmetic, geometric and harmonic means, together with confidence intervals.
<syntaxhighlight lang="text">clear all
set obs 10
gen x=_n
Line 3,405 ⟶ 3,406:
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">proc arithmeticMean list {
set sum 0.0
foreach value $list { set sum [expr {$sum + $value}] }
Line 3,438 ⟶ 3,439:
=={{header|Ursala}}==
 
<syntaxhighlight lang=Ursala"ursala">#import std
#import flo
 
Line 3,458 ⟶ 3,459:
Most valac setups will need "-X -lm" added to the compile command to include the C math library.
 
<syntaxhighlight lang="vala">
double arithmetic(int[] list){
double mean;
Line 3,522 ⟶ 3,523:
=={{header|VBA}}==
Uses Excel VBA.
<syntaxhighlight lang="vb">Private Function arithmetic_mean(s() As Variant) As Double
arithmetic_mean = WorksheetFunction.Average(s)
End Function
Line 3,543 ⟶ 3,544:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function arithmetic_mean(arr)
sum = 0
Line 3,582 ⟶ 3,583:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
 
Module Module1
Line 3,617 ⟶ 3,618:
=={{header|Vlang}}==
Updated for Vlang version 0.2.2
<syntaxhighlight lang="go">import math
 
fn main() {
Line 3,648 ⟶ 3,649:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascript">var rng = 1..10
var count = rng.count
var A = rng.reduce { |acc, x| acc + x }/count
Line 3,670 ⟶ 3,671:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">include c:\cxpl\codes;
 
func real Power(X, Y); \X raised to the Y power
Line 3,708 ⟶ 3,709:
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">ns:=T(1,2,3,4,5,6,7,8,9,10);
ns.sum(0.0)/ns.len(); // Arithmetic mean
ns.reduce('*,1.0).pow(1.0/ns.len()); // Geometric mean
Line 3,718 ⟶ 3,719:
3.41417
</pre>
 
[[Category:Geometry]]
10,327

edits