Dados dos números, primero calcule la media aritmética y la media geométrica de estos dos números. Usando la media aritmética y la media geométrica así calculadas, encuentre la media armónica entre los dos números.
Ejemplos:
Input : a = 2 b = 4 Output : 2.666 Input : a = 5 b = 15 Output : 7.500
Media aritmética: La media aritmética ‘AM’ entre dos números ayb es un número tal que AM-a = b-AM. Así, si nos dan estos dos números, la media aritmética AM = 1/2(a+b)
Media geométrica: La media geométrica ‘GM’ entre dos números ayb es un número tal que GM/a = b/GM. Así, si nos dan estos dos números, la media geométrica GM = sqrt(a*b)
Media armónica: La media armónica ‘HM’ entre dos números a y b es un número tal que 1/HM – 1/a = 1/ b – 1/HM. Entonces, si nos dan estos dos números, la media armónica HM = 2ab/a+b
Ahora, también sabemos que
C++
// C++ implementation of computation of // arithmetic mean, geometric mean // and harmonic mean #include <bits/stdc++.h> using namespace std; // Function to calculate arithmetic // mean, geometric mean and harmonic mean double compute(int a, int b) { double AM, GM, HM; AM = (a + b) / 2; GM = sqrt(a * b); HM = (GM * GM) / AM; return HM; } // Driver function int main() { int a = 5, b = 15; double HM = compute(a, b); cout << "Harmonic Mean between " << a << " and " << b << " is " << HM ; return 0; }
Java
// Java implementation of computation of // arithmetic mean, geometric mean // and harmonic mean import java.io.*; class GeeksforGeeks { // Function to calculate arithmetic // mean, geometric mean and harmonic mean static double compute(int a, int b) { double AM, GM, HM; AM = (a + b) / 2; GM = Math.sqrt(a * b); HM = (GM * GM) / AM; return HM; } // Driver function public static void main(String args[]) { int a = 5, b = 15; double HM = compute(a, b); String str = ""; str = str + HM; System.out.print("Harmonic Mean between " + a + " and " + b + " is " + str.substring(0, 5)); } }
Python3
# Python 3 implementation of computation # of arithmetic mean, geometric mean # and harmonic mean import math # Function to calculate arithmetic # mean, geometric mean and harmonic mean def compute( a, b) : AM = (a + b) / 2 GM = math.sqrt(a * b) HM = (GM * GM) / AM return HM # Driver function a = 5 b = 15 HM = compute(a, b) print("Harmonic Mean between " , a, " and ", b , " is " , HM ) # This code is contributed by Nikita Tiwari.
C#
// C# implementation of computation of // arithmetic mean, geometric mean // and harmonic mean using System; class GeeksforGeeks { // Function to calculate arithmetic // mean, geometric mean and harmonic mean static double compute(int a, int b) { double AM, GM, HM; AM = (a + b) / 2; GM = Math.Sqrt(a * b); HM = (GM * GM) / AM; return HM; } // Driver function public static void Main() { int a = 5, b = 15; double HM = compute(a, b); Console.WriteLine("Harmonic Mean between " + a + " and " + b + " is " +HM); } } // This code is contributed by mits
PHP
<?php // PHP implementation of computation of // arithmetic mean, geometric mean // and harmonic mean // Function to calculate arithmetic // mean, geometric mean and harmonic mean function compute( $a, $b) { $AM; $GM; $HM; $AM = ($a + $b) / 2; $GM = sqrt($a * $b); $HM = ($GM * $GM) / $AM; return $HM; } // Driver Code $a = 5; $b = 15; $HM = compute($a, $b); echo"Harmonic Mean between " .$a. " and " .$b. " is " .$HM ; return 0; // This code is contributed by nitin mittal. ?>
Javascript
<script> // Javascript implementation of computation // of arithmetic mean, geometric mean // and harmonic mean // Function to calculate arithmetic // mean, geometric mean and harmonic mean function compute(a, b) { var AM = (a + b) / 2; var GM = Math.sqrt(a * b); var HM = (GM * GM) / AM; return HM; } // Driver Code var a = 5; var b = 15; var HM = compute(a, b) document.write("Harmonic Mean between " + a + " and " + b + " is " + HM.toFixed(3)); // This code is contributed by bunnyram19 </script>
Producción:
Harmonic Mean between 5 and 15 is 7.500
Complejidad de tiempo: O (log (a * b)), para usar la función sqrt donde a y b representan los números enteros dados.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.