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

Published on 12 May 2024 09:40 PM

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

The Java code calculates the arithmetic mean, geometric mean, and harmonic mean of a list of numbers. It also checks if the arithmetic mean is greater than or equal to the geometric mean, and if the geometric mean is greater than or equal to the harmonic mean. The main function creates a list of numbers and prints the calculated means and the results of the comparisons.

  1. PythagoreanMeans class: This class contains three static methods to calculate the arithmetic mean, geometric mean, and harmonic mean of a list of numbers.

    • arithmeticMean: This method takes a list of numbers as input and returns the arithmetic mean, which is the sum of the numbers divided by the number of numbers. If the list is empty, the method returns Double.NaN (not a number).
    • geometricMean: This method takes a list of numbers as input and returns the geometric mean, which is the nth root of the product of the numbers, where n is the number of numbers. If the list is empty, the method returns Double.NaN.
  • harmonicMean: This method takes a list of numbers as input and returns the harmonic mean, which is the reciprocal of the average of the reciprocals of the numbers. If the list is empty or contains 0, the method returns Double.NaN.
  1. Main method: This is the entry point of the program. It creates a list of numbers and calls the static methods in the PythagoreanMeans class to calculate the arithmetic mean, geometric mean, and harmonic mean of the list. It then prints the calculated means and the results of the comparisons.

  2. Inside the main method, there is another class called Average. This class has three static methods to calculate the arithmetic mean, geometric mean, and harmonic mean.

    • arithmAverage: This method takes an array of numbers as input and returns the arithmetic mean. If the array is null or empty, the method returns 0.0.
    • geomAverage: This method takes an array of numbers as input and returns the geometric mean. If the array is null or empty, the method returns 0.0.
    • harmAverage: This method takes an array of numbers as input and returns the harmonic mean. If the array is null or empty, the method returns 0.0.
  3. The main method calls the static methods in the Average class to calculate the arithmetic mean, geometric mean, and harmonic mean of the list of numbers. It then prints the calculated means and the results of the comparisons.

Source code in the java programming language

import java.util.Arrays;
import java.util.List;

public class PythagoreanMeans {
    public static double arithmeticMean(List<Double> numbers) {
        if (numbers.isEmpty()) return Double.NaN;
        double mean = 0.0;
        for (Double number : numbers) {
            mean += number;
        }
        return mean / numbers.size();
    }

    public static double geometricMean(List<Double> numbers) {
        if (numbers.isEmpty()) return Double.NaN;
        double mean = 1.0;
        for (Double number : numbers) {
            mean *= number;
        }
        return Math.pow(mean, 1.0 / numbers.size());
    }

    public static double harmonicMean(List<Double> numbers) {
        if (numbers.isEmpty() || numbers.contains(0.0)) return Double.NaN;
        double mean = 0.0;
        for (Double number : numbers) {
            mean += (1.0 / number);
        }
        return numbers.size() / mean;
    }

    public static void main(String[] args) {
        Double[] array = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
        List<Double> list = Arrays.asList(array);
        double arithmetic = arithmeticMean(list);
        double geometric = geometricMean(list);
        double harmonic = harmonicMean(list);
        System.out.format("A = %f  G = %f  H = %f%n", arithmetic, geometric, harmonic);
        System.out.format("A >= G is %b, G >= H is %b%n", (arithmetic >= geometric), (geometric >= harmonic));
    }
}


   public static double arithmAverage(double array[]){
       if (array == null ||array.length == 0) {
         return 0.0;
      }
      else {
         return DoubleStream.of(array).average().getAsDouble();
      }
   }

    public static double geomAverage(double array[]){
      if (array == null ||array.length == 0) {
         return 0.0;
      }
      else {
         double aver = DoubleStream.of(array).reduce(1, (x, y) -> x * y);
         return   Math.pow(aver, 1.0 / array.length);
      }
   }

     public static double harmAverage(double array[]){
         if (array == null ||array.length == 0) {
         return 0.0;
      }
      else {
         double aver = DoubleStream.of(array)
                  // remove null values
                  .filter(n -> n > 0.0)
                  // generate 1/n array
                  .map( n-> 1.0/n)
                  // accumulating
                  .reduce(0, (x, y) -> x + y);
                  // just this reduce is not working- need to do in 2 steps
                 // .reduce(0, (x, y) -> 1.0/x + 1.0/y);
         return   array.length / aver ;
      }
   }


  

You may also check:How to resolve the algorithm Lucky and even lucky numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Comments step by step in the Arendelle programming language
You may also check:How to resolve the algorithm Jensen's Device step by step in the Maxima programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the GML programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the RPL programming language