Skip to content

[FEATURE REQUEST] Adding geometric mean in math #4338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lukasb1b opened this issue Aug 29, 2023 · 7 comments
Closed

[FEATURE REQUEST] Adding geometric mean in math #4338

lukasb1b opened this issue Aug 29, 2023 · 7 comments

Comments

@lukasb1b
Copy link
Contributor

What would you like to Propose?

I did not see a geometric mean function in maths.
I added a test feature in the attachment I obviuously would change it according to the rules of the repository if assigned to me.

Issue details

Missing geometric mean function in maths src/main/java/com/thealgorithms/maths

Additional Information

public class test {
public static void main(String[] args) {
int[] test = {3,4};
System.out.println(geometric_mean(test));
}

public static double geometric_mean(int[] num) {
    int result=0;
    for(int i:num){
        result+=i*i;
    }

    return Math.pow(result,1.0/ num.length);
}

}

@punit324
Copy link
Contributor

punit324 commented Aug 29, 2023

Hi @lukasb1b ,
The method you have proposed for geometric mean is seems to be wrong.
Correct formula is,
GM = (x₁ · x₂ · ... · xₙ)^1/n
So,

public static double geometric_mean(int[] num) {
    int result=1;
    for(int i:num){
        result*=i;
    }
    return Math.pow(result,1.0/ num.length);
}

@lukasb1b
Copy link
Contributor Author

Yeah your right @punit324

@punit324
Copy link
Contributor

punit324 commented Sep 3, 2023

@lukasb1b
Changes are merged into master. You can now close this issue as done.

@lukasb1b lukasb1b closed this as completed Sep 3, 2023
@raisahab9125
Copy link

raisahab9125 commented Sep 4, 2023 via email

@raisahab9125
Copy link

raisahab9125 commented Sep 4, 2023 via email

@lukasb1b
Copy link
Contributor Author

lukasb1b commented Sep 4, 2023

We already discussed this issue and its fixed and the right algorithm is merged

@punit324
Copy link
Contributor

punit324 commented Sep 5, 2023

@raisahab9125 I've used formulas from wiki.
Here's the links.
Arithmetic Mean
Geometric Mean
Harmonic Mean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants