0 Replies - 1639 Views - Last Post: 07 April 2012 - 07:07 AM

#1 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Calculate the Geometric Mean

Posted 07 April 2012 - 07:07 AM

Description: See Example usage in snippetThe geometric mean, in mathematics, is a type of mean or average, which indicates the central tendency or typical value of a set of numbers.
public class Means
{
    // Returns the geometric mean of a data set
    public static double geometricMean(double[] data)
    {
	double sum = data[0];

	for (int i = 1; i < data.length; i++)
	{
	    sum *= data[i];
	}

	return Math.pow(sum, 1.0 / data.length);
    }

    public static void main(String[] args)
    {
	double[] x = { 2, 3, 4, 5, 6, 8, 10, 11.53542 };

	System.out.println("Geometric Mean = " + Means.geometricMean(x));
    }
}

// OUTPUT

// Geometric Mean = 5.343268362096718


Is This A Good Question/Topic? 0
  • +

Page 1 of 1