Averages/Pythagorean means

From Rosetta Code
Revision as of 05:22, 20 February 2010 by rosettacode>Paddy3118 (New task and python solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Averages/Pythagorean means
You are encouraged to solve this task according to the task description, using any language you may know.

Compute all three of the Pythagorean means of the numbers 1..10.

  • The most common, Arithmetic mean is the sum of the numbers divided by their count, n:
  • The Geometric mean is the n'th root of the multiplication of all the numbers:

Show that for this set of positive numbers.

Python

<lang Python>>>> from operator import mul >>> from functools import reduce >>> def amean(num): return sum(num)/len(num)

>>> def gmean(num): return reduce(mul, num, 1)**(1/len(num))

>>> def hmean(num): return len(num)/sum(1/n for n in num)

>>> numbers = range(1,11) # 1..10 >>> amean(numbers), gmean(numbers), hmean(numbers) (5.5, 4.528728688116765, 3.414171521474055) >>> assert( amean(numbers) >= gmean(numbers) >= hmean(numbers) ) >>> </lang>