In this program the practical definition of a geometric mean is used.
The definition is : The average of the logarithmic values of a data set, converted back to a base 10 number.
I have found that base 10 is not required.
I have used base 2 and as far as I know, anything can be used.
The program has comments and since it’s a fairly simple program , its self explanatory
#This is the program to calculate geometric mean of a sequence.
#The sequence will be passed as an array as argument to the function
#this is the method to return the geometric mean of the sequence
y=Array.new #This array will be used to store logarithms of the numbers of the sequence
#this loop is used to find logs of numbers in sequence and sum them up in a variable called tmp
for i in 0..x.length-1
#end of i for loop
#this is to find average of the sum of logs taken.
#This is to convert logs back to original form
#Finally display the result !!
puts “Geometric mean is : “+gm.to_s
#end of mean_calculate method
#end of class
In this code, I have used a very simple sequence.
You may test it with a larger sequence.