Averages/Pythagorean means: Difference between revisions

m (→‎{{header|Phix}}: simplified, added syntax colouring, marked p2js compatible)
imported>Regattaguru
 
(19 intermediate revisions by 9 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 48 ⟶ 49:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
PROC InverseI(INT a,result)
Line 116 ⟶ 117:
HarmonicMean(a,10,result)
Print(" Harmonic mean=") PrintRE(result)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Pythagorean_means.png Screenshot from Atari 8-bit computer]
Line 126 ⟶ 127:
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">function arithmeticMean(v:Vector.<Number>):Number
{
var sum:Number = 0;
Line 150 ⟶ 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 195 ⟶ 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 210 ⟶ 211:
Float'Image (Geometric_Mean) & " >= " &
Float'Image (Harmonic_Mean));
end Main;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
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}}
<langsyntaxhighlight lang="algol68">main: (
INT count:=0;
LONG REAL f, sum:=0, prod:=1, resum:=0;
Line 242 ⟶ 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 257 ⟶ 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 292 ⟶ 293:
write( "Harmonic mean: ", harmonicMean( v, 1, 10 ) )
 
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 302 ⟶ 303:
=={{header|Amazing Hopper}}==
Think about "talk" programming...
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#include <hopper.h>
 
Line 363 ⟶ 364:
stats(HARMEAN)
back
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 398 ⟶ 399:
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
<lang APL>
arithmetic←{(+/⍵)÷⍴⍵}
geometric←{(×/⍵)*÷⍴⍵}
Line 411 ⟶ 412:
4.528728688
harmonic x
3.414171521</langsyntaxhighlight>
 
=={{header|AppleScript}}==
{{trans|JavaScript}}
<langsyntaxhighlight AppleScriptlang="applescript">-- arithmetic_mean :: [Number] -> Number
on arithmetic_mean(xs)
Line 515 ⟶ 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}}==
 
<langsyntaxhighlight lang="rebol">arithmeticMean: function [arr]->
average arr
 
Line 533 ⟶ 534:
print arithmeticMean 1..10
print geometricMean 1..10
print harmonicMean 1..10</langsyntaxhighlight>
 
{{out}}
Line 542 ⟶ 543:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">A := ArithmeticMean(1, 10)
G := GeometricMean(1, 10)
H := HarmonicMean(1, 10)
Line 582 ⟶ 583:
Sum += 1 / (a + A_Index - 1)
Return, n / Sum
}</langsyntaxhighlight>
Message box shows:
<pre>
Line 592 ⟶ 593:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">#!/usr/bin/awk -f
{
x = $1; # value of 1st column
Line 605 ⟶ 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 632 ⟶ 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 661 ⟶ 671:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
Line 668 ⟶ 678:
{{works with|C sharp|C#|3}}
 
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Diagnostics;
Line 701 ⟶ 711:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 709 ⟶ 719:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <vector>
#include <iostream>
#include <numeric>
Line 733 ⟶ 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 752 ⟶ 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 765 ⟶ 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 771 ⟶ 781:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun generic-mean (nums reduce-op final-op)
(funcall final-op (reduce reduce-op nums)))
 
Line 793 ⟶ 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 815 ⟶ 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 869 ⟶ 916:
else
writeln("Error!");
end.</langsyntaxhighlight>
 
=={{header|E}}==
Line 875 ⟶ 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 889 ⟶ 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 898 ⟶ 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 911 ⟶ 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 930 ⟶ 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 941 ⟶ 1,009:
=={{header|Erlang}}==
 
<langsyntaxhighlight Erlanglang="erlang">%% Author: Abhay Jain <abhay_1303@yahoo.co.in>
 
-module(mean_calculator).
Line 975 ⟶ 1,043:
harmonic_mean(Number, Sum) ->
NewSum = Sum + (1/Number),
harmonic_mean(Number+1, NewSum). </langsyntaxhighlight>
 
{{out}}
Line 983 ⟶ 1,051:
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM MEANS
 
Line 1,027 ⟶ 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 1,039 ⟶ 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 1,099 ⟶ 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 1,112 ⟶ 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 1,125 ⟶ 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 1,142 ⟶ 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 1,152 ⟶ 1,220:
 
: h-mean ( seq -- mean )
[ length ] [ [ recip ] map-sum ] bi / ;</langsyntaxhighlight>
 
( scratchpad ) 10 [1,b] [ a-mean ] [ g-mean ] [ h-mean ] tri
Line 1,160 ⟶ 1,228:
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 1,202 ⟶ 1,270:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: famean ( faddr n -- f )
0e
tuck floats bounds do
Line 1,231 ⟶ 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,250 ⟶ 1,318:
end if
 
end program Mean</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
' FB 1.05.0 Win64
 
Line 1,294 ⟶ 1,362:
Print "Press any key to quit the program"
Sleep
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,304 ⟶ 1,372:
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">import lists.zip
 
def
Line 1,319 ⟶ 1,387:
println( "$l: $m" + (if m is Rational then " or ${m.doubleValue()}" else '') )
 
println( monotone(means, (>=)) )</langsyntaxhighlight>
 
{{out}}
Line 1,332 ⟶ 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,346 ⟶ 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,366 ⟶ 1,460:
# 3.41417
geomean(v);
# 4.52873</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,386 ⟶ 1,480:
fmt.Println("A:", a, "G:", g, "H:", h)
fmt.Println("A >= G >= H:", a >= g && g >= h)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,395 ⟶ 1,489:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def arithMean = { list ->
list == null \
? null \
Line 1,417 ⟶ 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,431 ⟶ 1,525:
G: ${G}
H: ${H}
"""</langsyntaxhighlight>
 
{{out}}
Line 1,443 ⟶ 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,453 ⟶ 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,478 ⟶ 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,485 ⟶ 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,494 ⟶ 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,509 ⟶ 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,542 ⟶ 1,636:
}
return *L / m
end</langsyntaxhighlight>
 
{{out}}
Line 1,553 ⟶ 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,581 ⟶ 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,641 ⟶ 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,648 ⟶ 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,685 ⟶ 1,779:
}
}
</langsyntaxhighlight>
 
=={{header|JavaScript}}==
 
===ES5===
<langsyntaxhighlight lang="javascript">(function () {
'use strict';
 
Line 1,750 ⟶ 1,844:
 
})();
</syntaxhighlight>
</lang>
 
{{Out}}
<syntaxhighlight lang="javascript">{
<lang JavaScript>{
"values": {
"Arithmetic": 5.5,
Line 1,760 ⟶ 1,854:
},
"test": "is A >= G >= H ? yes"
}</langsyntaxhighlight>
 
===ES6===
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
 
// arithmeticMean :: [Number] -> Number
Line 1,825 ⟶ 1,919:
mean.Geometric >= mean.Harmonic ? "yes" : "no"}`
}, 2);
})();</langsyntaxhighlight>
{{Out}}
<syntaxhighlight lang="javascript">{
<lang JavaScript>{
"values": {
"Arithmetic": 5.5,
Line 1,834 ⟶ 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,849 ⟶ 1,943:
| ( $ans[],
"amean > gmean > hmean => \($ans[0] > $ans[1] and $ans[1] > $ans[2] )" )
</syntaxhighlight>
</lang>
{{Out}}
<pre>5.5
Line 1,859 ⟶ 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,874 ⟶ 1,968:
 
=={{header|K}}==
<syntaxhighlight lang="k">
<lang K>
am:{(+/x)%#x}
gm:{(*/x)^(%#x)}
Line 1,881 ⟶ 1,975:
{(am x;gm x;hm x)} 1+!10
5.5 4.528729 3.414172
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="kotlin">import kotlin.math.round
import kotlin.math.pow
 
Line 1,906 ⟶ 2,000:
println("A >= G is ${a >= g}, G >= H is ${g >= h}")
require(g in h..a)
}</langsyntaxhighlight>
{{out}}
<pre>A = 5.500000 G = 4.528729 H = 3.414172
Line 1,912 ⟶ 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,929 ⟶ 2,023:
arithmetic_mean(generateSeries(1,10)->asStaticArray)
geometric_mean(generateSeries(1,10)->asStaticArray)
harmonic_mean(generateSeries(1,10)->asStaticArray)</langsyntaxhighlight>
 
{{out}}
Line 1,937 ⟶ 2,031:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">for i = 1 to 10
a = a + i
next
Line 1,962 ⟶ 2,056:
print "False"
end if
</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to compute_means :count
local "sum
make "sum 0
Line 1,987 ⟶ 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 2,001 ⟶ 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 2,011 ⟶ 2,105:
 
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
sum=lambda -> {
Line 2,064 ⟶ 2,158:
}
CheckIt
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">x := [ seq( 1 .. 10 ) ];
Means := proc( x )
uses Statistics;
Line 2,075 ⟶ 2,169:
 
is( Arithmeticmean >= Geometricmean and Geometricmean >= Harmonicmean );
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,084 ⟶ 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 2,096 ⟶ 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 2,121 ⟶ 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,210 ⟶ 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,235 ⟶ 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,287 ⟶ 2,392:
-- problem here...
return numbers.size / mean
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,294 ⟶ 2,399:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import math, sequtils, sugar
proc amean(num: seq[float]): float =
Line 2,318 ⟶ 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,324 ⟶ 2,429:
=={{header|Oberon-2}}==
Oxford Oberon-2
<langsyntaxhighlight lang="oberon2">
MODULE PythMean;
IMPORT Out, ML := MathL;
Line 2,360 ⟶ 2,465:
END PythMean.
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,370 ⟶ 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,416 ⟶ 2,521:
return numbers->Size() / mean;
}
}</langsyntaxhighlight>
 
Output:
Line 2,428 ⟶ 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,440 ⟶ 2,545:
let nn = float_of_int n in
(!a /. nn, !b ** (1.0/.nn), nn /. !c)
;;</langsyntaxhighlight>
 
{{out}}
Line 2,448 ⟶ 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,456 ⟶ 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,469 ⟶ 2,574:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">import: mapping
 
: A ( x )
Line 2,485 ⟶ 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,495 ⟶ 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,535 ⟶ 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,570 ⟶ 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,585 ⟶ 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,603 ⟶ 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,611 ⟶ 2,716:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub A
{
my $a = 0;
Line 2,636 ⟶ 2,741:
 
print "A=$a\nG=$g\nH=$h\n";
die "Error" unless $a >= $g and $g >= $h;</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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,661 ⟶ 2,766:
<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>
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,671 ⟶ 2,776:
 
=={{header|PHP}}==
<langsyntaxhighlight PHPlang="php"><?php
// Created with PHP 7.0
 
Line 2,700 ⟶ 2,805:
echo "Geometric: " . GeometricMean($values) . "\n";
echo "Harmonic: " . HarmonicMean($values) . "\n";
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,709 ⟶ 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,723 ⟶ 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,730 ⟶ 2,835:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
declare n fixed binary,
(Average, Geometric, Harmonic) float;
Line 2,752 ⟶ 2,857:
if Average < Geometric then put skip list ('Error');
if Geometric < Harmonic then put skip list ('Error');
</syntaxhighlight>
</lang>
Results:
<pre>
Line 2,761 ⟶ 2,866:
 
=={{header|PostScript}}==
<syntaxhighlight lang="text">
/pythamean{
/x exch def
Line 2,784 ⟶ 2,889:
 
10 pythamean
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,794 ⟶ 2,899:
 
{{libheader|initlib}}
<langsyntaxhighlight lang="postscript">
/numbers {[1 10] 1 range}.
/recip {1 exch div}.
Line 2,804 ⟶ 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,829 ⟶ 2,934:
 
write-host "Is A >= G ? $($A -ge $G)"
write-host "Is G >= H ? $($G -ge $H)"</langsyntaxhighlight>
 
{{out}}
Line 2,839 ⟶ 2,944:
 
=={{header|Processing}}==
<langsyntaxhighlight Processinglang="processing">void setup() {
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
println("Arithmetic mean: " + arithmeticMean(numbers));
Line 2,871 ⟶ 2,976:
mean = nums.length / mean;
return mean;
}</langsyntaxhighlight>
{{out}}
<pre>Arithmetic mean: 5.5
Line 2,878 ⟶ 2,983:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.d ArithmeticMean()
For a = 1 To 10
mean + a
Line 2,903 ⟶ 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,926 ⟶ 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,974 ⟶ 3,107:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
Line 2,994 ⟶ 3,127:
(harmonic xs)
(>= (arithmetic xs) (geometric xs) (harmonic xs))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,007 ⟶ 3,140:
{{works with|Rakudo|2015.12}}
 
<syntaxhighlight lang="raku" perl6line>sub A { ([+] @_) / @_ }
sub G { ([*] @_) ** (1 / @_) }
sub H { @_ / [+] 1 X/ @_ }
Line 3,014 ⟶ 3,147:
say "G(1,...,10) = ", G(1..10);
say "H(1,...,10) = ", H(1..10);
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,024 ⟶ 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.*/
Line 3,060 ⟶ 3,193:
 
g= g * sign(ox); if oy<0 then g= 1 / g /*adjust for original X sign; neg. root*/
numeric digits oDigs; return g / 1 /*normalize to original decimal digits.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 3,070 ⟶ 3,203:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(8)
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Line 3,100 ⟶ 3,233:
next
return sum
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,106 ⟶ 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 3,137 ⟶ 3,297:
p h = (1..10).harmonic_mean
# is h < g < a ??
p g.between?(h, a)</langsyntaxhighlight>
 
{{out}}
Line 3,148 ⟶ 3,308:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">bXsum = 1
for i = 1 to 10
sum = sum + i ' sum of 1 -> 10
Line 3,163 ⟶ 3,323:
print " Harmonic Mean:";harmonic
if (average >= geometric) and (geometric >= harmonic) then print "True" else print "False"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,172 ⟶ 3,332:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn main() {
let mut sum = 0.0;
let mut prod = 1;
Line 3,188 ⟶ 3,348:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,196 ⟶ 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 3,209 ⟶ 3,369:
println("Harmonic mean " + h)
 
assert(a >= g && g >= h)</langsyntaxhighlight>
{{out}}
<pre>Arithmetic mean 5.5
Line 3,217 ⟶ 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,239 ⟶ 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,265 ⟶ 3,425:
writeln("Geometric mean: " <& product ** (1.0 / flt(length(numbers))));
writeln("Harmonic mean: " <& flt(length(numbers)) / reciprocalSum);
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,275 ⟶ 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,298 ⟶ 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,324 ⟶ 3,484:
 
((a arithmeticMean) >= (a geometricMean)) displayNl.
((a geometricMean) >= (a harmonicMean)) displayNl.</langsyntaxhighlight>
 
{{out}}
Line 3,335 ⟶ 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,354 ⟶ 3,514:
from
averages;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,364 ⟶ 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,374 ⟶ 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,399 ⟶ 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,410 ⟶ 3,606:
=={{header|Ursala}}==
 
<langsyntaxhighlight Ursalalang="ursala">#import std
#import flo
 
Line 3,421 ⟶ 3,617:
#cast %eLbX
 
main = ^(~&,ordered not fleq) <a,g,h></langsyntaxhighlight>
{{out}}
<pre>(
Line 3,430 ⟶ 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,483 ⟶ 3,679:
stdout.printf("Harmonic mean: %s\n", harmonic_mean.to_string());
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,494 ⟶ 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,509 ⟶ 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,515 ⟶ 3,711:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function arithmetic_mean(arr)
sum = 0
Line 3,543 ⟶ 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,554 ⟶ 3,750:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
 
Module Module1
Line 3,581 ⟶ 3,777:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Arithmetic mean 5.5
Line 3,587 ⟶ 3,783:
Harmonic mean 3.41417152147406</pre>
 
=={{header|V (Vlang)}}==
Updated for Vlang version 0.2.2
<langsyntaxhighlight lang="go">import math
 
fn main() {
Line 3,612 ⟶ 3,808:
compare := if a >= g && g >= h { "Yes" } else { "Nope" }
println('Is A >= G >= H? $compare')
}</langsyntaxhighlight>
{{out}}
<pre>Arithmetic Mean: 5.50
Line 3,620 ⟶ 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,630 ⟶ 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,642 ⟶ 3,838:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
 
func real Power(X, Y); \X raised to the Y power
Line 3,669 ⟶ 3,865:
Text(0, "ALWAYS DECREASING ORDER
");
]</langsyntaxhighlight>
 
{{out}}
Line 3,680 ⟶ 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,690 ⟶ 3,886:
3.41417
</pre>
 
[[Category:Geometry]]
Anonymous user