Averages/Pythagorean means: Difference between revisions

m (→‎{{header|Phix}}: iff went builtin in 2016!)
imported>Regattaguru
 
(29 intermediate revisions by 17 users not shown)
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}}==
<langsyntaxhighlight lang="11l">F amean(num)
R sum(num)/Float(num.len)
Line 37 ⟶ 38:
print(amean(numbers))
print(gmean(numbers))
print(hmean(numbers))</langsyntaxhighlight>
 
{{out}}
Line 44 ⟶ 45:
4.52873
3.41417
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
PROC InverseI(INT a,result)
REAL one,x
 
IntToReal(1,one)
IntToReal(a,x)
RealDiv(one,x,result)
RETURN
 
PROC ArithmeticMean(INT ARRAY a INT count REAL POINTER result)
INT i
REAL x,sum,tmp
 
IntToReal(0,sum)
FOR i=0 TO count-1
DO
IntToReal(a(i),x)
RealAdd(sum,x,tmp)
RealAssign(tmp,sum)
OD
IntToReal(count,tmp)
RealDiv(sum,tmp,result)
RETURN
 
PROC GeometricMean(INT ARRAY a INT count REAL POINTER result)
INT i
REAL x,prod,tmp
 
IntToReal(1,prod)
FOR i=0 TO count-1
DO
IntToReal(a(i),x)
RealMult(prod,x,tmp)
RealAssign(tmp,prod)
OD
InverseI(count,tmp)
Power(prod,tmp,result)
RETURN
 
PROC HarmonicMean(INT ARRAY a INT count REAL POINTER result)
INT i
REAL x,sum,tmp
 
IntToReal(0,sum)
FOR i=0 TO count-1
DO
InverseI(a(i),x)
RealAdd(sum,x,tmp)
RealAssign(tmp,sum)
OD
IntToReal(count,tmp)
RealDiv(tmp,sum,result)
RETURN
 
PROC Main()
BYTE i
INT ARRAY a=[1 2 3 4 5 6 7 8 9 10]
REAL result
 
Put(125) PutE() ;clear screen
 
ArithmeticMean(a,10,result)
Print("Arithmetic mean=") PrintRE(result)
GeometricMean(a,10,result)
Print(" Geometric mean=") PrintRE(result)
HarmonicMean(a,10,result)
Print(" Harmonic mean=") PrintRE(result)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Pythagorean_means.png Screenshot from Atari 8-bit computer]
<pre>
Arithmetic mean=5.5
Geometric mean=4.52872861
Harmonic mean=3.41417153
</pre>
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">function arithmeticMean(v:Vector.<Number>):Number
{
var sum:Number = 0;
Line 71 ⟶ 151:
trace("Arithmetic: ", arithmeticMean(list));
trace("Geometric: ", geometricMean(list));
trace("Harmonic: ", harmonicMean(list));</langsyntaxhighlight>
 
=={{header|Ada}}==
 
pythagorean_means.ads:
<langsyntaxhighlight Adalang="ada">package Pythagorean_Means is
type Set is array (Positive range <>) of Float;
function Arithmetic_Mean (Data : Set) return Float;
function Geometric_Mean (Data : Set) return Float;
function Harmonic_Mean (Data : Set) return Float;
end Pythagorean_Means;</langsyntaxhighlight>
 
pythagorean_means.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Generic_Elementary_Functions;
package body Pythagorean_Means is
package Math is new Ada.Numerics.Generic_Elementary_Functions (Float);
Line 116 ⟶ 196:
end Harmonic_Mean;
 
end Pythagorean_Means;</langsyntaxhighlight>
 
example main.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Pythagorean_Means;
procedure Main is
Line 131 ⟶ 211:
Float'Image (Geometric_Mean) & " >= " &
Float'Image (Harmonic_Mean));
end Main;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 138 ⟶ 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}}
<langsyntaxhighlight lang="algol68">main: (
INT count:=0;
LONG REAL f, sum:=0, prod:=1, resum:=0;
Line 163 ⟶ 243:
printf(($"Geometric mean = "f(real)l$,prod**(1/count)));
printf(($"Harmonic mean = "f(real)l$,count/resum))
)</langsyntaxhighlight>
Lunix command:
a68g Averages_Pythagorean_means.a68 - 1 2 3 4 5 6 7 8 9 10
Line 178 ⟶ 258:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight 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 213 ⟶ 293:
write( "Harmonic mean: ", harmonicMean( v, 1, 10 ) )
 
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 219 ⟶ 299:
Geometric mean: 4.52872
Harmonic mean: 3.41417
</pre>
 
=={{header|Amazing Hopper}}==
Think about "talk" programming...
<syntaxhighlight lang="amazing hopper">
#include <hopper.h>
 
/* An example of definitions in pseudo-natural language, with synonimous.
These definitions can be inside a definition file (xxxx.h) */
#define getasinglelistof(_X_) {_X_},
#synon getasinglelistof getalistof
#define integerrandomnumbers _V1000_=-1,rand array(_V1000_),mulby(10),ceil,
#define randomnumbers _V1000_=-1,rand array(_V1000_)
#define rememberitin(_X_) _X_=0,cpy(_X_)
#synon rememberitin rememberthisnumbersin
#define rememberas(_X_) mov(_X_)
#define remember(_X_) {_X_}
//#synon remember with ---> this exist in HOPPER.H
#defn nowconsiderthis(_X_) #ATOM#CMPLX,print
#synon nowconsiderthis nowconsider,considerthis,consider,nowputtext,puttext,andprint
#define andprintwithanewline {"\n"}print
#synon andprintwithanewline printwithanewline
//#defn andprint(_X_) #ATOM#CMPLX,print
#define putanewline {"\n"}
#define withanewline "\n"
#define andprintit print
#synon andprintit printit,andprint
#define showit show
#define afterdoingit emptystack?,not,do{ {"I cannot continue due to retentive data "},throw(1001) }
#synon afterdoingit secondly,finally
#define then emptystack?do{ {"I cannot continue because data is missing "},throw(1000) }
/* why "#context" and not "#define"?
becose "#context" need a value in the stack for continue.
Internally, "domeanit" tranform to "gosub(calculatearithmeticmean)",
and "gosub" works only if it finds a data in the stack */
#context calculatethegeometricmean
#synon calculatethegeometricmean calculategeometricmean,getgeometricmean
#context calculatetheharmonicmean
#synon calculatetheharmonicmean calculateharmonicmean,getharmonicmean
#context calculatearitmethicmean
#synon calculatearitmethicmean calculatesinglemean,calculatemean,domeanit
 
main:
consider this ("Arithmetic Mean: ")
get a list of '10,10' integer random numbers; remember this numbers in 'list of numbers';
then, do mean it, and print with a new line.
after doing it, consider ("Geometric Mean: "), remember 'list of numbers', calculate the geometric mean;
then, put a new line, and print it.
/*
Okay. This can be a bit long, if we have to write the program;
But what if we just had to talk, and the language interpreter takes care of the rest?
*/
secondly, now consider ("Harmonic Mean: "), with 'list of numbers', get harmonic mean, and print with a new line.
finally, put text ("Original Array:\n"), and print (list of numbers, with a new line)
exit(0)
.locals
calculate aritmethic mean:
stats(MEAN)
back
calculate the geometric mean:
stats(GEOMEAN)
back
calculatetheharmonicmean:
stats(HARMEAN)
back
</syntaxhighlight>
{{out}}
<pre>
Arithmetic Mean: 5.51
Geometric Mean: 4.47333
Harmonic Mean: 3.29515
Original Array:
4 3 10 6 10 1 1 10 4 1
7 4 5 8 9 7 3 8 1 10
5 5 2 5 8 9 1 5 10 10
6 4 3 5 9 2 6 9 2 9
10 9 3 4 6 2 1 8 10 1
7 5 5 9 9 3 7 9 7 7
9 2 10 1 7 9 3 2 7 4
1 6 2 4 10 7 5 1 5 5
1 2 9 5 10 7 7 6 6 4
3 4 5 2 4 1 10 6 3 7
</pre>
Notice:
<pre>
The line "stats(GEOMEAN)" is identical to:
stats(MULTIPLICATORY),POW BY({1}div by (100))
and...
#defn reciprocalof(_X_) {1},#ATOM#CMPLX,postfix,div,postfix
then:
stats(MULTIPLICATORY),POW BY( reciprocal of (100) )
or...
#defn N-ROOTof(_DATA_,_N_) #ATOM#CMPLX,{1}div by(_N_),postfix,pow,postfix
#define Multiplicatoryof(_DATA_) {_DATA_}stats(MULTIPLICATORY)
then:
N-ROOT of ( Multiplicatory of (list of numbers), 100)
etc.
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
<lang APL>
arithmetic←{(+/⍵)÷⍴⍵}
geometric←{(×/⍵)*÷⍴⍵}
Line 235 ⟶ 412:
4.528728688
harmonic x
3.414171521</langsyntaxhighlight>
 
=={{header|AppleScript}}==
{{trans|JavaScript}}
<langsyntaxhighlight AppleScriptlang="applescript">-- arithmetic_mean :: [Number] -> Number
on arithmetic_mean(xs)
Line 339 ⟶ 516:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight AppleScriptlang="applescript">{values:{arithmetic:5.5, geometric:4.528728688117, harmonic:3.414171521474},
inequalities:{|A >= G|:true}, |G >= H|:true}</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">arithmeticMean: function [arr]->
average arr
 
geometricMean: function [arr]->
(product arr) ^ 1//size arr
 
harmonicMean: function [arr]->
(size arr) // sum map arr 'i [1//i]
 
print arithmeticMean 1..10
print geometricMean 1..10
print harmonicMean 1..10</syntaxhighlight>
 
{{out}}
 
<pre>5.5
4.528728688116765
3.414171521474055</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">A := ArithmeticMean(1, 10)
G := GeometricMean(1, 10)
H := HarmonicMean(1, 10)
Line 385 ⟶ 583:
Sum += 1 / (a + A_Index - 1)
Return, n / Sum
}</langsyntaxhighlight>
Message box shows:
<pre>
Line 395 ⟶ 593:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">#!/usr/bin/awk -f
{
x = $1; # value of 1st column
Line 408 ⟶ 606:
print "Geometric mean : ",exp(G/N);
print "Harmonic mean : ",N/H;
}</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
The arithmetic and harmonic means use BBC BASIC's built-in array operations; only the geometric mean needs a loop.
<langsyntaxhighlight lang="bbcbasic"> DIM a(9)
a() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
PRINT "Arithmetic mean = " ; FNarithmeticmean(a())
Line 435 ⟶ 633:
b() = 1/a()
= (DIM(a(),1)+1) / SUM(b())
</syntaxhighlight>
</lang>
{{out}}
<pre>Arithmetic mean = 5.5
Geometric mean = 4.52872869
Harmonic mean = 3.41417152</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">A ← +´÷≠
G ← ≠√×´
H ← A⌾÷
 
⋈⟜(∧´ ¯1⊸↓ ≥ 1⊸↓) (A∾G∾H) 1+↕10</syntaxhighlight>
{{out}}
<pre>⟨ ⟨ 5.5 4.528728688116765 3.414171521474055 ⟩ 1 ⟩</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h> // atoi()
#include <math.h> // pow()
Line 464 ⟶ 671:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
Line 471 ⟶ 678:
{{works with|C sharp|C#|3}}
 
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Diagnostics;
Line 504 ⟶ 711:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 512 ⟶ 719:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <vector>
#include <iostream>
#include <numeric>
Line 536 ⟶ 743:
<< geometric_mean << " and the harmonic mean " << harmonic_mean << " !\n" ;
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>The arithmetic mean is 5.5 , the geometric mean 4.52873 and the harmonic mean 3.41417 !</pre>
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(use '[clojure.contrib.math :only (expt)])
(defn a-mean [coll]
Line 555 ⟶ 762:
a (a-mean numbers) g (g-mean numbers) h (h-mean numbers)]
(println a ">=" g ">=" h)
(>= a g h))</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight 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 568 ⟶ 775:
 
console.log "A = ", A, " G = ", G, " H = ", H
console.log "A >= G : ", A >= G, " G >= H : ", G >= H</langsyntaxhighlight>
{{out}}
<pre>A = 5.5 G = 4.528728688116765 H = 3.414171521474055
Line 574 ⟶ 781:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun generic-mean (nums reduce-op final-op)
(funcall final-op (reduce reduce-op nums)))
 
Line 596 ⟶ 803:
(format t "a-mean ~a~%" a-mean)
(format t "g-mean ~a~%" g-mean)
(format t "h-mean ~a~%" h-mean)))</langsyntaxhighlight>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">precision 6
 
define bxsum = 1, sum = 0, sum1i = 0
define average = 0, geometric = 0, harmonic = 0
 
for i = 1 to 10
 
let sum = sum + i
let bxsum = bxsum * i
let sum1i = sum1i + (1 / i)
 
next i
 
let average = sum / 10
let geometric = bxsum ^ (1 / 10)
let harmonic = 10 / sum1i
 
print "arithmetic mean: ", average
print "geometric mean: ", geometric
print "harmonic mean: ", harmonic
 
if average >= geometric and geometric >= harmonic then
 
print "true"
end
 
endif
 
print "false"</syntaxhighlight>
{{out| Output}}<pre>
arithmetic mean: 5.500000
geometric mean: 4.528729
harmonic mean: 3.414172
true
</pre>
 
=={{header|D}}==
The output for the harmonic mean is wrong.
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.functional;
 
auto aMean(T)(T data) pure nothrow @nogc {
Line 618 ⟶ 862:
writefln("%(%.19f %)", m);
assert(m.isSorted);
}</langsyntaxhighlight>
{{out}}
<pre>0.9891573712076470036 4.5287286881167647619 5.5000000000000000000</pre>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program AveragesPythagoreanMeans;
 
{$APPTYPE CONSOLE}
Line 672 ⟶ 916:
else
writeln("Error!");
end.</langsyntaxhighlight>
 
=={{header|E}}==
Line 678 ⟶ 922:
Given that we're defining all three together, it makes sense to express their regularities:
 
<langsyntaxhighlight lang="e">def makeMean(base, include, finish) {
return def mean(numbers) {
var count := 0
Line 692 ⟶ 936:
def A := makeMean(0, fn b,x { b+x }, fn acc,n { acc / n })
def G := makeMean(1, fn b,x { b*x }, fn acc,n { acc ** (1/n) })
def H := makeMean(0, fn b,x { b+1/x }, fn acc,n { n / acc })</langsyntaxhighlight>
 
<langsyntaxhighlight lang="e">? A(1..10)
# value: 5.5
 
Line 701 ⟶ 945:
 
? H(1..10)
# value: 3.414171521474055</langsyntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
proc mean . v[] a g h .
prod = 1
for v in v[]
sum += v
prod *= v
resum += 1 / v
.
a = sum / len v[]
g = pow prod (1 / len v[])
h = len v[] / resum
.
a[] = [ 1 2 3 4 5 6 7 8 9 10 ]
mean a[] a g h
print a
print g
print h
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(define (A xs) (// (for/sum ((x xs)) x) (length xs)))
 
Line 714 ⟶ 979:
(and (>= (A xs) (G xs)) (>= (G xs) (H xs)))
→ #t
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Means do
def arithmetic(list) do
Enum.sum(list) / length(list)
Line 733 ⟶ 998:
IO.puts "Geometric mean: #{gm = Means.geometric(list)}"
IO.puts "Harmonic mean: #{hm = Means.harmonic(list)}"
IO.puts "(#{am} >= #{gm} >= #{hm}) is #{am >= gm and gm >= hm}"</langsyntaxhighlight>
{{out}}
<pre>
Line 744 ⟶ 1,009:
=={{header|Erlang}}==
 
<langsyntaxhighlight Erlanglang="erlang">%% Author: Abhay Jain <abhay_1303@yahoo.co.in>
 
-module(mean_calculator).
Line 778 ⟶ 1,043:
harmonic_mean(Number, Sum) ->
NewSum = Sum + (1/Number),
harmonic_mean(Number+1, NewSum). </langsyntaxhighlight>
 
{{out}}
Line 786 ⟶ 1,051:
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM MEANS
 
Line 830 ⟶ 1,095:
PRINT("Harmonic mean = ";M)
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>function A(x) := mean(x)
>function G(x) := exp(mean(log(x)))
Line 842 ⟶ 1,107:
4.52872868812
3.41417152147
</syntaxhighlight>
</lang>
 
Alternatively, e.g.,
 
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>function G(x) := prod(x)^(1/length(x))
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function arithmetic_mean(sequence s)
atom sum
if length(s) = 0 then
Line 902 ⟶ 1,167:
printf(1,"Harmonic: %g\n", harmonic)
printf(1,"Arithmetic>=Geometric>=Harmonic: %s\n",
{true_or_false(arithmetic>=geometric and geometric>=harmonic)})</langsyntaxhighlight>
 
{{out}}
Line 915 ⟶ 1,180:
Use the functions : AVERAGE, GEOMEAN and HARMEAN
 
<syntaxhighlight lang="excel">
<lang Excel>
=AVERAGE(1;2;3;4;5;6;7;8;9;10)
=GEOMEAN(1;2;3;4;5;6;7;8;9;10)
=HARMEAN(1;2;3;4;5;6;7;8;9;10)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 928 ⟶ 1,193:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight 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 945 ⟶ 1,210:
printfn "Arithmetic Mean: %A" (arithmeticMean P)
printfn "Geometric Mean: %A" (geometricMean P)
printfn "Harmonic Mean: %A" (harmonicMean P)</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">: a-mean ( seq -- mean )
[ sum ] [ length ] bi / ;
 
Line 955 ⟶ 1,220:
 
: h-mean ( seq -- mean )
[ length ] [ [ recip ] map-sum ] bi / ;</langsyntaxhighlight>
 
( scratchpad ) 10 [1,b] [ a-mean ] [ g-mean ] [ h-mean ] tri
Line 963 ⟶ 1,228:
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 1,005 ⟶ 1,270:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: famean ( faddr n -- f )
0e
tuck floats bounds do
Line 1,034 ⟶ 1,299:
test 10 fhmean fdup f.
( A G G H )
f>= . f>= . \ -1 -1</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90}}
<langsyntaxhighlight lang="fortran">program Mean
 
real :: a(10) = (/ (i, i=1,10) /)
Line 1,053 ⟶ 1,318:
end if
 
end program Mean</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
' FB 1.05.0 Win64
 
Line 1,097 ⟶ 1,362:
Print "Press any key to quit the program"
Sleep
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,107 ⟶ 1,372:
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">import lists.zip
 
def
Line 1,122 ⟶ 1,387:
println( "$l: $m" + (if m is Rational then " or ${m.doubleValue()}" else '') )
 
println( monotone(means, (>=)) )</langsyntaxhighlight>
 
{{out}}
Line 1,135 ⟶ 1,400:
=={{header|Futhark}}==
 
<syntaxhighlight lang="futhark">
<lang Futhark>
fun arithmetic_mean(as: [n]f64): f64 =
reduce (+) 0.0 (map (/f64(n)) as)
Line 1,149 ⟶ 1,414:
geometric_mean as,
harmonic_mean as)
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="FutureBasic>
 
Double ari = 1, geo = 0, har = 0
Short i, n = 10
 
for i = 1 to n
ari += i
geo *= i
har += 1 \ i
next
 
print "ari:", ari \ n
print "geo:", geo^( 1 \ n )
print "har:", n \ har
 
handleevents
</syntaxhighlight>
<pre>
 
ari: 5.5
geo: 4.528728688116765
har: 3.414171521474055
 
</pre>
 
=={{header|GAP}}==
<langsyntaxhighlight 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,169 ⟶ 1,460:
# 3.41417
geomean(v);
# 4.52873</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,189 ⟶ 1,480:
fmt.Println("A:", a, "G:", g, "H:", h)
fmt.Println("A >= G >= H:", a >= g && g >= h)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,198 ⟶ 1,489:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def arithMean = { list ->
list == null \
? null \
Line 1,220 ⟶ 1,511:
? 0 \
: list.size() / list.collect { 1.0/it }.sum()
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">def list = 1..10
def A = arithMean(list)
def G = geomMean(list)
Line 1,234 ⟶ 1,525:
G: ${G}
H: ${H}
"""</langsyntaxhighlight>
 
{{out}}
Line 1,246 ⟶ 1,537:
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>.
 
<langsyntaxhighlight lang="haskell">import Data.List (genericLength)
import Control.Monad (zipWithM_)
 
Line 1,256 ⟶ 1,547:
let ms = zipWith ((. flip mean [1..10]). (,)) "agh" [1, 0, -1]
mapM_ (\(t,m) -> putStrLn $ t : ": " ++ show m) ms
putStrLn $ " a >= g >= h is " ++ show ((\(_,[a,g,h])-> a>=g && g>=h) (unzip ms))</langsyntaxhighlight>
 
====Three applicatively defined functions====
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.
 
<langsyntaxhighlight lang="haskell">import Data.List (genericLength)
 
-- ARITHMETIC, GEOMETRIC AND HARMONIC MEANS ---------------
Line 1,281 ⟶ 1,572:
, mappend "\n A >= G >= H is " $ --
(show . and) $ zipWith (>=) xs (tail xs)
]</langsyntaxhighlight>
{{Out}}
<pre>("Arithmetic",5.5)("Geometric",4.528728688116765)("Harmonic",3.414171521474055)
Line 1,288 ⟶ 1,579:
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">AGH = ALIAS( A, G, H ) ! named vector elements
AGH = (0, 1, 0)
DO i = 1, 10
Line 1,297 ⟶ 1,588:
AGH = (A/10, G^0.1, 10/H)
 
WRITE(ClipBoard, Name) AGH, "Result = " // (A>=G) * (G>=H)</langsyntaxhighlight>
! A=5.5; G=4.528728688; H=3.414171521; Result = 1;
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">link numbers # for a/g/h means
 
procedure main()
Line 1,312 ⟶ 1,603:
write(" a >= g >= h is ", if a >= g >= h then "true" else "false")
end
</syntaxhighlight>
</lang>
 
{{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:
<langsyntaxhighlight Iconlang="icon">procedure amean(L[]) #: arithmetic mean
local m
if *L = 0 then fail
Line 1,345 ⟶ 1,636:
}
return *L / m
end</langsyntaxhighlight>
 
{{out}}
Line 1,356 ⟶ 1,647:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "Averages.bas"
110 NUMERIC ARR(1 TO 10)
120 FOR I=LBOUND(ARR) TO UBOUND(ARR)
Line 1,384 ⟶ 1,675:
360 NEXT
370 LET HARMONIC=SIZE(A)/T
380 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">amean=: +/ % #
gmean=: # %: */
hmean=: amean&.:%</langsyntaxhighlight>
 
'''Example Usage:'''
<langsyntaxhighlight 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</langsyntaxhighlight>
 
Note that gmean could have instead been defined as mean under logarithm, for example:
 
<langsyntaxhighlight lang="j">gmean=:amean&.:^.</langsyntaxhighlight>
 
(and this variant should probably be preferred - especially if the argument list is long, to avoid problems with floating point infinity.)
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.Arrays;
import java.util.List;
 
Line 1,444 ⟶ 1,735:
System.out.format("A >= G is %b, G >= H is %b%n", (arithmetic >= geometric), (geometric >= harmonic));
}
}</langsyntaxhighlight>
{{out}}
<pre>A = 5.500000 G = 4.528729 H = 3.414172
Line 1,451 ⟶ 1,742:
{{works with|Java|1.8}}
We can rewrite the 3 methods using the new JAVA Stream API:
<langsyntaxhighlight lang="java">
public static double arithmAverage(double array[]){
if (array == null ||array.length == 0) {
Line 1,488 ⟶ 1,779:
}
}
</langsyntaxhighlight>
 
=={{header|JavaScript}}==
 
===ES5===
<langsyntaxhighlight lang="javascript">(function () {
'use strict';
 
Line 1,553 ⟶ 1,844:
 
})();
</syntaxhighlight>
</lang>
 
{{Out}}
<syntaxhighlight lang="javascript">{
<lang JavaScript>{
"values": {
"Arithmetic": 5.5,
Line 1,563 ⟶ 1,854:
},
"test": "is A >= G >= H ? yes"
}</langsyntaxhighlight>
 
===ES6===
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
 
// arithmeticMean :: [Number] -> Number
Line 1,628 ⟶ 1,919:
mean.Geometric >= mean.Harmonic ? "yes" : "no"}`
}, 2);
})();</langsyntaxhighlight>
{{Out}}
<syntaxhighlight lang="javascript">{
<lang JavaScript>{
"values": {
"Arithmetic": 5.5,
Line 1,637 ⟶ 1,928:
},
"test": "is A >= G >= H ? yes"
}</langsyntaxhighlight>
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">def amean: add/length;
 
def logProduct: map(log) | add;
Line 1,652 ⟶ 1,943:
| ( $ans[],
"amean > gmean > hmean => \($ans[0] > $ans[1] and $ans[1] > $ans[2] )" )
</syntaxhighlight>
</lang>
{{Out}}
<pre>5.5
Line 1,662 ⟶ 1,953:
Julia has a `mean` function to compute the arithmetic mean of a collections
of numbers. We can redefine it as follows.
<langsyntaxhighlight Julialang="julia">amean(A) = sum(A)/length(A)
 
gmean(A) = prod(A)^(1/length(A))
 
hmean(A) = length(A)/sum(1./A)</langsyntaxhighlight>
{{Out}}
<pre>julia> map(f-> f(1:10), [amean, gmean, hmean])
Line 1,677 ⟶ 1,968:
 
=={{header|K}}==
<syntaxhighlight lang="k">
<lang K>
am:{(+/x)%#x}
gm:{(*/x)^(%#x)}
Line 1,684 ⟶ 1,975:
{(am x;gm x;hm x)} 1+!10
5.5 4.528729 3.414172
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">import kotlin.math.round
<lang scala>fun Collection<Double>.geometricMean() =
import kotlin.math.pow
if (isEmpty()) Double.NaN
else Math.pow(reduce { n1, n2 -> n1 * n2 }, 1.0 / size)
 
fun Collection<Double>.geometricMean() =
if (isEmpty()) Double.NaN
else (reduce { n1, n2 -> n1 * n2 }).pow(1.0 / size)
fun Collection<Double>.harmonicMean() =
if (isEmpty() || contains(0.0)) Double.NaN
else size / reducefold(0.0) { n1, n2 -> n1 + 1.0 / n2 }
 
fun mainDouble.toFixed(argslen: Array<String>Int = 6) {=
round(this * 10.0.pow(len)) / 10.0.pow(len)
fun main() {
val list = listOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
val a = list.average() // arithmetic mean
val g = list.geometricMean()
val h = list.harmonicMean()
println("A = %f$a G = %f${g.toFixed()} H = %f"${h.formattoFixed(a, g, h)}")
println("A >= G is %b${a >= g}, G >= H is %b".format(a >= g, ${g >= h)}")
require(g in h..a)
}</langsyntaxhighlight>
{{out}}
<pre>A = 5.500000 G = 4.528729 H = 3.414172
Line 1,709 ⟶ 2,006:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="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,726 ⟶ 2,023:
arithmetic_mean(generateSeries(1,10)->asStaticArray)
geometric_mean(generateSeries(1,10)->asStaticArray)
harmonic_mean(generateSeries(1,10)->asStaticArray)</langsyntaxhighlight>
 
{{out}}
Line 1,734 ⟶ 2,031:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">for i = 1 to 10
a = a + i
next
Line 1,759 ⟶ 2,056:
print "False"
end if
</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to compute_means :count
local "sum
make "sum 0
Line 1,784 ⟶ 2,081:
print sentence [Geometric mean is] item 2 :means
print sentence [Harmonic mean is] item 3 :means
bye</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight 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 1,798 ⟶ 2,095:
h = pymean(nums, function(n) return 1/n end, function(n) return 1/n end)
print(a, g, h)
assert(a >= g and g >= h)</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 1,808 ⟶ 2,105:
 
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
sum=lambda -> {
Line 1,861 ⟶ 2,158:
}
CheckIt
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">x := [ seq( 1 .. 10 ) ];
Means := proc( x )
uses Statistics;
Line 1,872 ⟶ 2,169:
 
is( Arithmeticmean >= Geometricmean and Geometricmean >= Harmonicmean );
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,881 ⟶ 2,178:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Print["{Arithmetic Mean, Geometric Mean, Harmonic Mean} = ",
N@Through[{Mean, GeometricMean, HarmonicMean}[Range@10]]]</langsyntaxhighlight>
{{out}}
<pre>{Arithmetic Mean, Geometric Mean, Harmonic Mean} = {5.5,4.52873,3.41417}</pre>
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function [A,G,H] = pythagoreanMeans(list)
A = mean(list);
Line 1,893 ⟶ 2,190:
H = harmmean(list);
end</langsyntaxhighlight>
 
A solution that works for both, Matlab and Octave, is this
 
<langsyntaxhighlight MATLABlang="matlab">function [A,G,H] = pythagoreanMeans(list)
A = mean(list); % arithmetic mean
G = exp(mean(log(list))); % geometric mean
H = 1./mean(1./list); % harmonic mean
end</langsyntaxhighlight>
 
Solution:
<langsyntaxhighlight MATLABlang="matlab">>> [A,G,H]=pythagoreanMeans((1:10))
 
A =
Line 1,918 ⟶ 2,215:
H =
 
3.414171521474055</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">/* built-in */
L: makelist(i, i, 1, 10)$
 
mean(L), numer; /* 5.5 */
geometric_mean(L), numer; /* 4.528728688116765 */
harmonic_mean(L), numer; /* 3.414171521474055 */</langsyntaxhighlight>
 
=={{header|min}}==
<syntaxhighlight lang="min">'avg ^A
(dup product 1 rolldown size / pow) ^G
('size keep (1 swap /) (+) map-reduce /) ^H
 
(((1 10)) range (((A) (G) (H))) cleave) => (puts!) foreach</syntaxhighlight>
{{out}}
<pre>5.5
4.528728688116765
3.414171521474055</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE PythagoreanMeans;
FROM FormatString IMPORT FormatString;
FROM LongMath IMPORT power;
Line 2,007 ⟶ 2,315:
 
ReadChar
END PythagoreanMeans.</langsyntaxhighlight>
 
=={{header|MUMPS}}==
 
<langsyntaxhighlight MUMPSlang="mumps">Pyth(n) New a,ii,g,h,x
For ii=1:1:n set x(ii)=ii
;
Line 2,032 ⟶ 2,340:
Pythagorean means for 1..10:
Average = 5.5 >= Geometric 4.528728688116178495 >= harmonic 3.414171521474055006</langsyntaxhighlight>
 
=={{header|NetRexx}}==
{{trans|ooRexx}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,084 ⟶ 2,392:
-- problem here...
return numbers.size / mean
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,091 ⟶ 2,399:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import math, sequtils, sugar
proc amean(num: seq[float]): float =
Line 2,115 ⟶ 2,423:
let numbers = toSeq(1..10).map((x: int) => float(x))
echo amean(numbers), " ", gmean(numbers), " ", hmean(numbers)</langsyntaxhighlight>
{{out}}
<pre>5.5 4.528728688116765 3.414171521474055</pre>
Line 2,121 ⟶ 2,429:
=={{header|Oberon-2}}==
Oxford Oberon-2
<langsyntaxhighlight lang="oberon2">
MODULE PythMean;
IMPORT Out, ML := MathL;
Line 2,157 ⟶ 2,465:
END PythMean.
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,167 ⟶ 2,475:
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight 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,213 ⟶ 2,521:
return numbers->Size() / mean;
}
}</langsyntaxhighlight>
 
Output:
Line 2,225 ⟶ 2,533:
The three means in one function
 
<langsyntaxhighlight lang="ocaml">let means v =
let n = Array.length v
and a = ref 0.0
Line 2,237 ⟶ 2,545:
let nn = float_of_int n in
(!a /. nn, !b ** (1.0/.nn), nn /. !c)
;;</langsyntaxhighlight>
 
{{out}}
Line 2,245 ⟶ 2,553:
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:
 
<langsyntaxhighlight lang="ocaml">let means v =
let (a, b, c) =
Array.fold_left
Line 2,253 ⟶ 2,561:
let n = float_of_int (Array.length v) in
(a /. n, b ** (1./.n), n /. c)
;;</langsyntaxhighlight>
 
=={{header|Octave}}==
<syntaxhighlight lang="octave">
<lang Octave>
A = mean(list); % arithmetic mean
G = mean(list,'g'); % geometric mean
H = mean(list,'a'); % harmonic mean
</syntaxhighlight>
</lang>
 
See also Matlab implementation [[#MATLAB]]
Line 2,266 ⟶ 2,574:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">import: mapping
 
: A ( x )
Line 2,282 ⟶ 2,590:
"Arithmetic mean :" . 10 seq A dup . g >= ifTrue: [ " ==> A >= G" .cr ]
"Harmonic mean :" . 10 seq H dup . g <= ifTrue: [ " ==> G >= H" .cr ]
;</langsyntaxhighlight>
 
{{out}}
Line 2,292 ⟶ 2,600:
 
=={{header|ooRexx}}==
<langsyntaxhighlight ooRexxlang="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,332 ⟶ 2,640:
return numbers~items / mean
 
::requires rxmath LIBRARY</langsyntaxhighlight>
{{out}}
<pre>Arithmetic = 5.5, Geometric = 4.52872869, Harmonic = 3.41417153</pre>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
%% helpers
fun {Sum Xs} {FoldL Xs Number.'+' 0.0} end
Line 2,367 ⟶ 2,675:
{Show [A G H]}
A >= G = true
G >= H = true</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
General implementations:
<langsyntaxhighlight lang="parigp">arithmetic(v)={
sum(i=1,#v,v[i])/#v
};
Line 2,382 ⟶ 2,690:
 
v=vector(10,i,i);
[arithmetic(v),geometric(v),harmonic(v)]</langsyntaxhighlight>
 
Specific to the first ''n'' positive integers:
<langsyntaxhighlight lang="parigp">arithmetic_first(n)={
(n+1)/2
};
Line 2,400 ⟶ 2,708:
 
[arithmetic_first(10),geometric_first(10),harmonic_first(10)]
%[1]>=%[2] && %[2] >= %[3]</langsyntaxhighlight>
 
These are, asymptotically, n/2, n/e, and n/log n.
Line 2,408 ⟶ 2,716:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub A
{
my $a = 0;
Line 2,433 ⟶ 2,741:
 
print "A=$a\nG=$g\nH=$h\n";
die "Error" unless $a >= $g and $g >= $h;</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function arithmetic_mean(sequence s)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
return sum(s)/length(s)
<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>
end function
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</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>
function geometric_mean(sequence s)
atom p = 1
<span style="color: #008080;">function</span> <span style="color: #000000;">geometric_mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for i=1 to length(s) do
<span style="color: #008080;">return</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</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>
p *= s[i]
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end for
return power(p,1/length(s))
<span style="color: #008080;">function</span> <span style="color: #000000;">harmonic_mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</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: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</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>
function harmonic_mean(sequence s)
atom rsum = 0
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</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>
for i=1 to length(s) do
<span style="color: #008080;">constant</span> <span style="color: #000000;">arithmetic</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">arithmetic_mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span>
rsum += 1/s[i]
<span style="color: #000000;">geometric</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">geometric_mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span>
end for
<span style="color: #000000;">harmonic</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">harmonic_mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
return length(s)/rsum
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Arithmetic: %.10g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">arithmetic</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Geometric: %.10g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">geometric</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Harmonic: %.10g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">harmonic</span><span style="color: #0000FF;">)</span>
constant s = {1,2,3,4,5,6,7,8,9,10}
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Arithmetic&gt;=Geometric&gt;=Harmonic: %t\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">arithmetic</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">geometric</span> <span style="color: #008080;">and</span> <span style="color: #000000;">geometric</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">harmonic</span><span style="color: #0000FF;">})</span>
constant arithmetic = arithmetic_mean(s),
<!--</syntaxhighlight>-->
geometric = geometric_mean(s),
harmonic = harmonic_mean(s)
printf(1,"Arithmetic: %.10g\n", arithmetic)
printf(1,"Geometric: %.10g\n", geometric)
printf(1,"Harmonic: %.10g\n", harmonic)
printf(1,"Arithmetic>=Geometric>=Harmonic: %s\n", {iff((arithmetic>=geometric and geometric>=harmonic),"true","false")})</lang>
{{out}}
<pre>
Line 2,473 ⟶ 2,776:
 
=={{header|PHP}}==
<langsyntaxhighlight PHPlang="php"><?php
// Created with PHP 7.0
 
Line 2,502 ⟶ 2,805:
echo "Geometric: " . GeometricMean($values) . "\n";
echo "Harmonic: " . HarmonicMean($values) . "\n";
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,511 ⟶ 2,814:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="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,525 ⟶ 2,828:
(format
(*/ (* 1.0 Len) 1.0 (sum '((N) (*/ 1.0 1.0 N)) Lst))
*Scl ) ) )</langsyntaxhighlight>
{{out}}
<pre>Arithmetic mean: 5.500000
Line 2,532 ⟶ 2,835:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
declare n fixed binary,
(Average, Geometric, Harmonic) float;
Line 2,554 ⟶ 2,857:
if Average < Geometric then put skip list ('Error');
if Geometric < Harmonic then put skip list ('Error');
</syntaxhighlight>
</lang>
Results:
<pre>
Line 2,563 ⟶ 2,866:
 
=={{header|PostScript}}==
<syntaxhighlight lang="text">
/pythamean{
/x exch def
Line 2,586 ⟶ 2,889:
 
10 pythamean
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,596 ⟶ 2,899:
 
{{libheader|initlib}}
<langsyntaxhighlight lang="postscript">
/numbers {[1 10] 1 range}.
/recip {1 exch div}.
Line 2,606 ⟶ 2,909:
% Harmonic mean
numbers dup 0 {recip +} fold exch length exch div
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
<langsyntaxhighlight PowerShelllang="powershell">$A = 0
$LogG = 0
$InvH = 0
Line 2,631 ⟶ 2,934:
 
write-host "Is A >= G ? $($A -ge $G)"
write-host "Is G >= H ? $($G -ge $H)"</langsyntaxhighlight>
 
{{out}}
Line 2,639 ⟶ 2,942:
Is A >= G ? True
Is G >= H ? True</pre>
 
=={{header|Processing}}==
<syntaxhighlight lang="processing">void setup() {
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
println("Arithmetic mean: " + arithmeticMean(numbers));
println("Geometric mean: " + geometricMean(numbers));
println("Harmonic mean: " + harmonicMean(numbers));
}
 
float arithmeticMean(float[] nums) {
float mean = 0;
for (float n : nums) {
mean += n;
}
mean = mean / nums.length;
return mean;
}
 
float geometricMean(float[] nums) {
float mean = 1;
for (float n : nums) {
mean *= n;
}
mean = pow(mean, 1.0 / nums.length);
return mean;
}
 
float harmonicMean(float[] nums) {
float mean = 0;
for (float n : nums) {
mean += 1 / n;
}
mean = nums.length / mean;
return mean;
}</syntaxhighlight>
{{out}}
<pre>Arithmetic mean: 5.5
Geometric mean: 4.528729
Harmonic mean: 3.4141712</pre>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.d ArithmeticMean()
For a = 1 To 10
mean + a
Line 2,666 ⟶ 3,008:
Debug ArithmeticMean()
Debug GeometricMean()
Debug HarmonicMean()</langsyntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|3}}
<langsyntaxhighlight Pythonlang="python">from operator import mul
from functools import reduce
 
Line 2,689 ⟶ 3,031:
a, g, h = amean(numbers), gmean(numbers), hmean(numbers)
print(a, g, h)
assert a >= g >= h</langsyntaxhighlight>
{{out}}
<pre>5.5 4.52872868812 3.41417152147</pre>
 
These are the same in Python 2 apart from requiring explicit float division (either through <code>float()</code> casts or float literals such as <code>1./n</code>); 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.
 
=={{header|Quackery}}==
 
Uses <code>root</code> from [[Integer roots#Quackery]].
 
<syntaxhighlight lang="quackery"> [] 10 times [ i^ 1+ join ]
 
say "Arithmetic mean:" sp
0 over witheach +
over size 8 point$ echo$
cr
say " Geometric mean:" sp
1 over witheach *
over size 80 ** * 10 root
10 8 ** 8 point$ echo$
cr
say " Harmonic mean:" sp
dup size dip
[ 0 n->v rot
witheach [ n->v 1/v v+ ] ]
n->v 2swap v/ 8 point$ echo$</syntaxhighlight>
 
{{out}}
 
<pre>Arithmetic mean: 5.5
Geometric mean: 4.52872868
Harmonic mean: 3.41417152
</pre>
 
=={{header|R}}==
Initialise x
<syntaxhighlight lang="r">
<lang R>
x <- 1:10
</syntaxhighlight>
</lang>
Arithmetic mean
<syntaxhighlight lang="r">
<lang R>
a <- sum(x)/length(x)
 
</syntaxhighlight>
</lang>
or
<syntaxhighlight lang="r">
<lang R>
a <- mean(x)
</syntaxhighlight>
</lang>
 
The geometric mean
<syntaxhighlight lang="r">
<lang R>
g <- prod(x)^(1/length(x))
</syntaxhighlight>
</lang>
 
The harmonic mean (no error checking that <math>x_i\ne 0,\text{ } \forall \text{ }i=1\ldots n</math>)
<syntaxhighlight lang="r">
<lang R>
h <- length(x)/sum(1/x)
</syntaxhighlight>
</lang>
 
Then:
 
<syntaxhighlight lang="r">
<lang R>
a > g
</syntaxhighlight>
</lang>
 
and
 
<syntaxhighlight lang="r">
<lang R>
g > h
</syntaxhighlight>
</lang>
 
give both
Line 2,737 ⟶ 3,107:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
Line 2,757 ⟶ 3,127:
(harmonic xs)
(>= (arithmetic xs) (geometric xs) (harmonic xs))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,770 ⟶ 3,140:
{{works with|Rakudo|2015.12}}
 
<syntaxhighlight lang="raku" perl6line>sub A { ([+] @_) / @_ }
sub G { ([*] @_) ** (1 / @_) }
sub H { @_ / [+] 1 X/ @_ }
Line 2,777 ⟶ 3,147:
say "G(1,...,10) = ", G(1..10);
say "H(1,...,10) = ", H(1..10);
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,787 ⟶ 3,157:
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.
<langsyntaxhighlight 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.*/
if n=='' | n=="," then n=10 10 /*None specified? Then use the default*/
sum= 0; prod= 1; rSum=0 0 /*initialize sum/product/reciprocal sum*/
$=; do #=1 for n; $= $ # /*generate list by appending # to list.*/
sum = sum + # /*compute the sum of all the elements. */
prod= prod * # /*compute the product of all elements. */
rSum= rSum + 1/# /*compute the sum of the reciprocals. */
end /*#*/
say ' list ='$ /*display the list of numbers used. */
say 'Amean =' sum / n /*calculate & display arithmetic mean.*/
say 'Gmean =' Iroot(prod, n) /* " " " geometric " */
if result=="[n/a]" then say '***error***: root' y "can't be even if 1st argument is < 0."
say 'Hmean =' n / rSum /* " " " harmonic " */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Iroot: procedure; parse arg x 1 ox, y 1 oy /*get both args, and also a copy of X&Y*/
if x=0 | x=1 | y=1 then return x /*handle special case of zero and unity*/
if y=0 if y=0 then return 1 then return 1 /* " " " " a zero root.*/
if x<0 & y//2==0 then return IrootErr() '[n/a]' /*indicate result is "not applicable". */
x= abs(x); y= abs(y); m= y - 1 /*use the absolute value for X and Y. */
oDigs= digits(); a= oDigs + 5 /*save original digits; add five digs.*/
g= (x+1) / y**y 2 /*use this as the first guesstimate. */
d=5 d= 5 /*start with 5 dec digs, saves CPU time*/
do until d==a do until d==a; d= min(d + d, a) /*keep going as digits are increased. */
d=min(d+d, a); numeric digits d; f= d - 2 /*limit digits to original digits + 5.*/
og= og= /*use a non-guessnon─guess for the old G (guess)*/
do forever; gm=g**m do forever; gm= g**m /*keep computing at the Yth root. */
_= format( (m*g*gm + x) / (y*gm), , f) /*this is the nitty─gritty calculation.*/
if _=g | _=og then leave /*are we close enough yet? */
og=g; g=_ og= g; g= _ /*save guess ──► OG; set the new guess.*/
end /*forever*/
end end /*until */
 
if oy<0 then g=1/g g= g * sign(ox); if oy<0 then g= 1 / g /*use reciprocal whenadjust for Yoriginal X issign; negativeneg. root*/
numeric digits oDigs; return sign(ox)* g / 1 /*normalize to original decimal digits.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
/*──────────────────────────────────────────────────────────────────────────────────────*/
IrootErr: say '***error*** (from Iroot): root' y "can't be even if 1st argument is < 0."
return '[n/a]' /*return a "not applicable" string. */</lang>
'''output''' &nbsp; using the default inputs:
<pre>
list = 1 2 3 4 5 6 7 8 9 10
Line 2,835 ⟶ 3,203:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(8)
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Line 2,865 ⟶ 3,233:
next
return sum
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,871 ⟶ 3,239:
geometric mean = 4.52872869
harmonic mean = 3.41417152
</pre>
 
=={{header|RPL}}==
These words can be used either on vectors or lists.
{{works with|HP|28}}
≪ → array op
≪ array 1 GET 2 array SIZE
'''IF''' DUP2 > '''THEN''' DROP2 '''ELSE FOR''' j array GET op EVAL '''NEXT END'''
≫ ≫ '<span style="color:blue">REDUCE</span>' STO
≪ DUP ≪ + ≫ <span style="color:blue">REDUCE</span> SWAP SIZE /
≫ '<span style="color:blue">AMEAN</span>' STO
≪ DUP ≪ * ≫ <span style="color:blue">REDUCE</span> SWAP SIZE INV ^
≫ '<span style="color:blue">GMEAN</span>' STO
≪ SIZE LAST ≪ INV + ≫ <span style="color:blue">REDUCE</span> /
≫ '<span style="color:blue">HMEAN</span>' STO
 
{ 1 2 3 4 5 6 7 8 9 0 } <span style="color:blue">AMEAN</span>
{ 1 2 3 4 5 6 7 8 9 0 } <span style="color:blue">GMEAN</span>
[ 1 2 3 4 5 6 7 8 9 0 ] <span style="color:blue">HMEAN</span>
{{out}}
<pre>
3: 5.5
2: 4.52872868812
1: 3.41417152147
</pre>
 
=={{header|Ruby}}==
{{works with|Ruby|1.9+}}
<langsyntaxhighlight lang="ruby">class Array
def arithmetic_mean
inject(0.0, :+) / length
Line 2,902 ⟶ 3,297:
p h = (1..10).harmonic_mean
# is h < g < a ??
p g.between?(h, a)</langsyntaxhighlight>
 
{{out}}
Line 2,913 ⟶ 3,308:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">bXsum = 1
for i = 1 to 10
sum = sum + i ' sum of 1 -> 10
Line 2,928 ⟶ 3,323:
print " Harmonic Mean:";harmonic
if (average >= geometric) and (geometric >= harmonic) then print "True" else print "False"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,937 ⟶ 3,332:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn main() {
let mut sum = 0.0;
let mut prod = 1;
Line 2,953 ⟶ 3,348:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,961 ⟶ 3,356:
=={{header|Scala}}==
{{works with|Scala|2.8+}}
<langsyntaxhighlight 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 2,974 ⟶ 3,369:
println("Harmonic mean " + h)
 
assert(a >= g && g >= h)</langsyntaxhighlight>
{{out}}
<pre>Arithmetic mean 5.5
Line 2,982 ⟶ 3,377:
=={{header|Scheme}}==
{{Works with|Scheme|R<math>^5</math>RS}}
<langsyntaxhighlight lang="scheme">(define (a-mean l)
(/ (apply + l) (length l)))
 
Line 3,004 ⟶ 3,399:
(newline)
(display (>= a g h))
(newline))</langsyntaxhighlight>
{{out}}
<syntaxhighlight lang="text">11/2 >= 4.528728688116765 >= 25200/7381
#t</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 3,030 ⟶ 3,425:
writeln("Geometric mean: " <& product ** (1.0 / flt(length(numbers))));
writeln("Harmonic mean: " <& flt(length(numbers)) / reciprocalSum);
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,040 ⟶ 3,435:
 
=={{header|Sidef}}==
<langsyntaxhighlight 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 }</langsyntaxhighlight>
 
The same thing, using hyper-operators:
<langsyntaxhighlight lang="sidef">func A(a) { a«+» / a.len }
func G(a) { a«*» ** (1/a.len) }
func H(a) { a.len / (a«/«1 «+») }</langsyntaxhighlight>
 
Calling the functions:
<langsyntaxhighlight lang="sidef">say("A(1,...,10) = ", A(1..10));
say("G(1,...,10) = ", G(1..10));
say("H(1,...,10) = ", H(1..10));</langsyntaxhighlight>
{{out}}
<pre>A(1,...,10) = 5.5
Line 3,063 ⟶ 3,458:
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 /.
 
<langsyntaxhighlight lang="smalltalk">Collection extend
[
arithmeticMean
Line 3,089 ⟶ 3,484:
 
((a arithmeticMean) >= (a geometricMean)) displayNl.
((a geometricMean) >= (a harmonicMean)) displayNl.</langsyntaxhighlight>
 
{{out}}
Line 3,100 ⟶ 3,495:
=={{header|SQL}}==
It may not be possible to calculate a geometric mean in a query, but the other two are easy enough.
<langsyntaxhighlight lang="sql">
--setup
create table averages (val integer);
Line 3,119 ⟶ 3,514:
from
averages;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,129 ⟶ 3,524:
=={{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,139 ⟶ 3,534:
| Geometric 10 4.528729 2.680672 7.650836
| Harmonic 10 3.414172 2.035664 10.57602
-----------------------------------------------------------------------------</langsyntaxhighlight>
=={{header|Swift}}==
<syntaxhighlight lang="text">
// Utility for easy creation of Double from any Numeric
extension Double {
init(withNum v: any Numeric) {
switch v {
case let ii as any BinaryInteger: self.init(ii)
case let ff as any BinaryFloatingPoint: self.init(ff)
default: self.init()
}
}
}
// Extension for numeric collections
extension Collection where Element: Numeric {
var arithmeticMean: Double {
self.reduce(0.0, {$0 + Double(withNum: $1)})/Double(self.count)
}
var geometricMean: Double {
pow(self.reduce(1.0, {$0 * Double(withNum: $1)}), 1.0/Double(self.count))
}
var harmonicMean: Double {
Double(self.count) / self.reduce(0.0, {$0 + 1.0/Double(withNum:$1)})
}
}
//Usage:
var c: [Int] = (1...10).map {$0}
 
print(c.arithmeticMean)
print(c.geometricMean)
print(c.harmonicMean)
 
// output:
// 5.5
// 4.528728688116765
// 3.414171521474055
</syntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc arithmeticMean list {
set sum 0.0
foreach value $list { set sum [expr {$sum + $value}] }
Line 3,164 ⟶ 3,595:
puts "A10=$A10, G10=$G10, H10=$H10"
if {$A10 >= $G10} { puts "A10 >= G10" }
if {$G10 >= $H10} { puts "G10 >= H10" }</langsyntaxhighlight>
 
{{out}}
Line 3,175 ⟶ 3,606:
=={{header|Ursala}}==
 
<langsyntaxhighlight Ursalalang="ursala">#import std
#import flo
 
Line 3,186 ⟶ 3,617:
#cast %eLbX
 
main = ^(~&,ordered not fleq) <a,g,h></langsyntaxhighlight>
{{out}}
<pre>(
Line 3,195 ⟶ 3,626:
Most valac setups will need "-X -lm" added to the compile command to include the C math library.
 
<langsyntaxhighlight lang="vala">
double arithmetic(int[] list){
double mean;
Line 3,248 ⟶ 3,679:
stdout.printf("Harmonic mean: %s\n", harmonic_mean.to_string());
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,259 ⟶ 3,690:
=={{header|VBA}}==
Uses Excel VBA.
<langsyntaxhighlight lang="vb">Private Function arithmetic_mean(s() As Variant) As Double
arithmetic_mean = WorksheetFunction.Average(s)
End Function
Line 3,274 ⟶ 3,705:
Debug.Print "G ="; geometric_mean(s)
Debug.Print "H ="; harmonic_mean(s)
End Sub</langsyntaxhighlight>{{out}}
<pre>A = 5,5
G = 4,52872868811677
Line 3,280 ⟶ 3,711:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function arithmetic_mean(arr)
sum = 0
Line 3,308 ⟶ 3,739:
WScript.StdOut.WriteLine geometric_mean(Array(1,2,3,4,5,6,7,8,9,10))
WScript.StdOut.WriteLine harmonic_mean(Array(1,2,3,4,5,6,7,8,9,10))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 3,319 ⟶ 3,750:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
 
Module Module1
Line 3,346 ⟶ 3,777:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Arithmetic mean 5.5
Line 3,352 ⟶ 3,783:
Harmonic mean 3.41417152147406</pre>
 
=={{header|V (Vlang)}}==
Updated for Vlang version 0.2.2
<lang vlang>import (
<syntaxhighlight lang="go">import math
arrays
math
)
 
fn main() {
Line 3,362 ⟶ 3,791:
mut prod :=1.0
mut recip_sum := 0.0
n := 10.0
for val in arrays1..range<int>(1, n + 1) {
sum += val
prod *= val
recip_sum = recip_sum + ( 1.0 / val )
}
a := sum / n
g := math.pow( prod, ( 1.0 / f32(n) ) )
h := n / recip_sum
result := 'Arithmetic Mean: ${a : 3.2f} \nGeometric Mean: ${g : 3.2f}\nHarmonic Mean: ${h : 3.2f}'
println( result )
xcompare := if a >= g && g >= h { "Yes" } else { "Nope" }
println('Is A >= G >= H? $xcompare')
}</langsyntaxhighlight>
{{out}}
<pre>Arithmetic Mean: 5.50
Line 3,387 ⟶ 3,816:
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var rng = 1..10
var count = rng.count
var A = rng.reduce { |acc, x| acc + x }/count
Line 3,397 ⟶ 3,826:
System.print(" Geometric mean = %(G)")
System.print(" Harmonic mean = %(H)")
System.print(" A >= G >= H = %(A >= G && G >= H)")</langsyntaxhighlight>
 
{{out}}
Line 3,409 ⟶ 3,838:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
 
func real Power(X, Y); \X raised to the Y power
Line 3,436 ⟶ 3,865:
Text(0, "ALWAYS DECREASING ORDER
");
]</langsyntaxhighlight>
 
{{out}}
Line 3,447 ⟶ 3,876:
 
=={{header|zkl}}==
<langsyntaxhighlight 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
ns.len().toFloat() / ns.reduce(fcn(p,n){ p + 1.0/n },0.0); // Harmonic mean</langsyntaxhighlight>
{{out}}
<pre>
Line 3,457 ⟶ 3,886:
3.41417
</pre>
 
[[Category:Geometry]]
Anonymous user