2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
									
										
										
										
											2019-07-29 07:14:15 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: Averages/Pythagorean means
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								id: 594d966a1467eb84194f0086
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302227
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Description
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'description' >  
						 
					
						
							
								
									
										
										
										
											2019-05-23 13:57:59 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Compute all three of the < a  class = 'rosetta__link--wiki'  href = 'https://en.wikipedia.org/wiki/Pythagorean means'  title = 'wp: Pythagorean means'  target = "_blank" > Pythagorean means< / a >  of the set of integers < big > 1< / big >  through < big > 10< / big >  (inclusive).
							 
						 
					
						
							
								
									
										
										
										
											2019-02-24 19:04:29 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Show that < big > $A(x_1,\ldots,x_n) \geq G(x_1,\ldots,x_n) \geq H(x_1,\ldots,x_n)$</ big >  for this set of positive integers.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< ul >  
						 
					
						
							
								
									
										
										
										
											2019-05-22 23:30:29 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < li > The most common of the three means, the < a  class = 'rosetta__link--rosetta'  href = 'https://rosettacode.org/wiki/Averages/Arithmetic mean'  title = 'Averages/Arithmetic mean'  target = '_blank' > arithmetic mean< / a > , is the sum of the list divided by its length:< br > 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-25 13:36:09 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < big > $ A(x_1, \ldots, x_n) = \frac{x_1 + \cdots + x_n}{n}$</ big ></ li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-01 17:10:50 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < li > The < a  class = 'rosetta__link--wiki'  href = 'https://en.wikipedia.org/wiki/Geometric mean'  title = 'wp: Geometric mean'  target = '_blank' > geometric mean< / a >  is the $n$th root of the product of the list:< br > 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-25 13:36:09 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < big > $ G(x_1, \ldots, x_n) = \sqrt[n]{x_1 \cdots x_n} $</ big ></ li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-01 17:10:50 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < li > The < a  class = 'rosetta__link--wiki'  href = 'https://en.wikipedia.org/wiki/Harmonic mean'  title = 'wp: Harmonic mean'  target = '_blank' > harmonic mean< / a >  is $n$ divided by the sum of the reciprocal of each item in the list:< br > 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-25 13:36:09 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  < big > $ H(x_1, \ldots, x_n) = \frac{n}{\frac{1}{x_1} + \cdots + \frac{1}{x_n}} $</ big ></ li > 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-24 19:04:29 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / ul >  
						 
					
						
							
								
									
										
										
										
											2019-03-01 17:10:50 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Instructions
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'instructions' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When writing your function, assume the input is an ordered array of all inclusive numbers.
							 
						 
					
						
							
								
									
										
										
										
											2019-02-24 19:04:29 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For the answer, please output an object in the following format:
							 
						 
					
						
							
								
									
										
										
										
											2019-05-23 13:57:59 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  values: {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Arithmetic: 5.5,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Geometric: 4.528728688116765,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Harmonic: 3.414171521474055
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  test: 'is A >= G >= H ? yes'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2019-05-23 13:57:59 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Tests
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'tests' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```yml
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								tests:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: < code > pythagoreanMeans</ code >  is a function.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(typeof pythagoreanMeans === 'function');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > pythagoreanMeans([1, 2, ..., 10])</ code >  should equal the same output above.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-26 05:24:52 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert.deepEqual(pythagoreanMeans(range1), answer1);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Challenge Seed
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'challengeSeed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-seed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-02-26 17:07:07 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function pythagoreanMeans(rangeArr) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  // Good luck!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### After Test
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-teardown' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const range1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const answer1 = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  values: {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Arithmetic: 5.5,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Geometric: 4.528728688116765,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Harmonic: 3.414171521474055
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  test: 'is A >= G >= H ? yes'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Solution
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'solution' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-02-26 17:07:07 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function pythagoreanMeans(rangeArr) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  // arithmeticMean :: [Number] -> Number
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const arithmeticMean = xs =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    foldl((sum, n) => sum + n, 0, xs) / length(xs);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // geometricMean :: [Number] -> Number
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const geometricMean = xs =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    raise(foldl((product, x) => product * x, 1, xs), 1 / length(xs));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // harmonicMean :: [Number] -> Number
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const harmonicMean = xs =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    length(xs) / foldl((invSum, n) => invSum + (1 / n), 0, xs);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // GENERIC FUNCTIONS ------------------------------------------------------
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // A list of functions applied to a list of arguments
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // < *> :: [(a -> b)] -> [a] -> [b]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const ap = (fs, xs) => //
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Array.prototype.concat(...fs.map(f => //
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      Array.prototype.concat(...xs.map(x => [f(x)]))));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // foldl :: (b -> a -> b) -> b -> [a] -> b
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const foldl = (f, a, xs) => xs.reduce(f, a);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // length :: [a] -> Int
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const length = xs => xs.length;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // mapFromList :: [(k, v)] -> Dictionary
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const mapFromList = kvs =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    foldl((a, [k, v]) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      (a[(typeof k === 'string' & &  k)] = v, a), {}, kvs);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // raise :: Num -> Int -> Num
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const raise = (n, e) => Math.pow(n, e);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/*
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // show :: a -> String
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // show :: a -> Int -> String
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const show = (...x) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    JSON.stringify.apply(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      null, x.length > 1 ? [x[0], null, x[1]] : x
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								*/
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // zip :: [a] -> [b] -> [(a,b)]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const zip = (xs, ys) =>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    xs.slice(0, Math.min(xs.length, ys.length))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      .map((x, i) => [x, ys[i]]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // TEST -------------------------------------------------------------------
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // mean :: Dictionary
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const mean = mapFromList(zip(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ['Arithmetic', 'Geometric', 'Harmonic'],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ap([arithmeticMean, geometricMean, harmonicMean], [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      rangeArr
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ])
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    values: mean,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test: `is A >= G >= H ? ${mean.Arithmetic >= mean.Geometric & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      mean.Geometric >= mean.Harmonic ? 'yes' : 'no'}`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >