JavaScript Program to Find Geometric mean in G.P.
Last Updated :
03 Apr, 2024
The geometric mean in a geometric progression (G.P.) is computed as the nth root of the product of all terms, where n is the number of terms. It provides a measure of central tendency in exponential sequences. This calculation is crucial in various mathematical and statistical analyses and we will see multiple approaches to do so.
If there are n elements x1, x2, x3, . . ., xn in an array and if we want to calculate the
geometric mean of the array elements is Geometric mean = (x1 * x2 * x3 * . . . * xn)1/n
Examples:
Input : arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output : 3.76435
= (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8)1/8
= 403201/8
= 3.76435
Input : arr[] = {15, 12, 13, 19, 10}
Output : 13.447
= (15 * 12 * 13 * 19 * 10)1/5
= 4446001/5
= 13.477
Using Math.pow and Array.reduce
In this approach, we will calculate the geometric mean by first finding the product of all numbers in the array using Array.reduce
, then applying the geometric mean formula using Math.pow
.
Example: To demonstrate finding Geometric mean in G.P. using predefined methods in javascript.
JavaScript
function geometricMean(arr) {
let product = arr.reduce((acc, curr) => acc * curr, 1);
return Math.pow(product, 1 / arr.length);
}
// Example usage
let numbers = [4, 10, 16, 24];
let mean = geometricMean(numbers);
console.log('Geometric Mean:', mean);
OutputGeometric Mean: 11.132630734854963
Time Complexity: O(n) where n is size of given array
Auxiliary Space: O(1)
Using a For Loop
In this approach, we will find the geometric mean by iterating through the array and multiplying all elements to calculate the product, then applying the geometric mean formula to determine the mean value.
Example: To demonstrate finding Geometric mean in G.P. using for loop.
JavaScript
function geometricMean(arr) {
let product = 1;
for (let i = 0; i < arr.length; i++) {
product *= arr[i];
}
return Math.pow(product, 1 / arr.length);
}
// Example usage
let numbers = [3, 9, 27, 81];
let mean = geometricMean(numbers);
console.log('Geometric Mean:', mean);
OutputGeometric Mean: 15.588457268119896
Time Complexity: O(n) where n is size of given array
Auxiliary Space: O(1)
Similar Reads
JavaScript Program to find nth term of Geometric Progression Geometric Progression is a sequence of numbers in which each term is obtained by multiplying the previous term by a constant number called the common ratio. In this article, we will learn how to find the nth term of Geometric Progression using JavaScript. We will explore various approaches that can
4 min read
JavaScript Program for nth Term of Geometric Progression In this article, we are going to learn how to find the nth term of Geometric Progression using JavaScript. We will see different approaches that will return the nth term of G.P. Geometric Progression is a sequence of numbers whose next term is calculated by multiplying the current term with a fixed
4 min read
Javascript Program for Mean of range in array Given an array of n integers. You are given q queries. Write a program to print the floor value of mean in range l to r for each query in a new line.Examples : Input : arr[] = {1, 2, 3, 4, 5} q = 3 0 2 1 3 0 4 Output : 2 3 3 Here for 0 to 2 (1 + 2 + 3) / 3 = 2 Input : arr[] = {6, 7, 8, 10} q = 2 0 3
3 min read
PHP Program to Find Sum of Geometric Series Given a number n, the task is to find the sum of the geometric series in PHP. A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and the common ratio is denoted by r. PHP Program to Sum of Geometric Series using for LoopA Simpl
2 min read
JavaScript Program for Sum of n terms of Geometric Progression Geometric Progression is a sequence of numbers whose next term is calculated by multiplying the current term with a fixed number known as a common ratio. We are going to learn how we can find the sum of n in terms of Geometric Progression in different ways using JavaScript including iteration, recur
3 min read
JavaScript Program for Sum of Infinite Terms in Geometric Progression Geometric Progression is a sequence of numbers whose next term is calculated by multiplying the current term with a fixed number known as the common ratio. Below are the approaches to find the sum of infinite terms in a Geometric progression using JavaScript: Table of Content Using Iterative Approac
3 min read