How to resolve the algorithm Averages/Pythagorean means step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Averages/Pythagorean means step by step in the Scala programming language
Table of Contents
Problem Statement
Compute all three of the Pythagorean means of the set of integers 1 through 10 (inclusive). Show that
A (
x
1
, … ,
x
n
) ≥ G (
x
1
, … ,
x
n
) ≥ H (
x
1
, … ,
x
n
)
{\displaystyle A(x_{1},\ldots ,x_{n})\geq G(x_{1},\ldots ,x_{n})\geq H(x_{1},\ldots ,x_{n})}
for this set of positive integers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Averages/Pythagorean means step by step in the Scala programming language
Source code in the scala programming language
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
var nums = 1 to 10
var a = arithmeticMean(nums)
var g = geometricMean(nums)
var h = harmonicMean(nums)
println("Arithmetic mean " + a)
println("Geometric mean " + g)
println("Harmonic mean " + h)
assert(a >= g && g >= h)
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the Ruby programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Perl programming language
You may also check:How to resolve the algorithm Knapsack problem/Bounded step by step in the OOCalc programming language
You may also check:How to resolve the algorithm Bernoulli numbers step by step in the Julia programming language
You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the C# programming language