How to resolve the algorithm Averages/Pythagorean means step by step in the OCaml programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Pythagorean means step by step in the OCaml 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 OCaml programming language

Source code in the ocaml programming language

let means v =
  let n = Array.length v
  and a = ref 0.0
  and b = ref 1.0
  and c = ref 0.0 in
  for i=0 to n-1 do
    a := !a +. v.(i);
    b := !b *. v.(i);
    c := !c +. 1.0/.v.(i);
  done;
  let nn = float_of_int n in
  (!a /. nn, !b ** (1.0/.nn), nn /. !c)
;;


let means v =
  let (a, b, c) =
    Array.fold_left
      (fun (a, b, c) x -> (a+.x, b*.x, c+.1./.x))
      (0.,1.,0.) v
  in
  let n = float_of_int (Array.length v) in
  (a /. n, b ** (1./.n), n /. c)
;;


  

You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the B programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the PL/I programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the RPL programming language
You may also check:How to resolve the algorithm Gotchas step by step in the C programming language