Geometric Mean Calculator

  • Share this:

Code introduction


Calculate the geometric mean of a set of numbers. The geometric mean is the nth root of the product of the numbers, which is suitable for calculating the average of positive numbers.


Technology Stack : math

Code Type : Mathematical calculation

Code Difficulty : Intermediate


                
                    
import os
import sys
import time
import random
import json
import re
import datetime
import hashlib
import math
import statistics

def calculate_geometric_mean(numbers):
    def geometric_mean(nums):
        product = math.prod(nums)
        return product ** (1.0 / len(nums))
    
    if not numbers or any(n <= 0 for n in numbers):
        return None
    return geometric_mean(numbers)                
              
Tags: