Reputation: 1
Is there a fast algorithm for calculating a harmonic mean of numbers? Not just summing all of the values in the denominator. How can we speed up this? In python3
Upvotes: 0
Views: 751
Reputation: 889
The naive approach would be O(n).
numbers = [3, 2, 6, 5, 1, 8] # example numbers
add = 0
for i in numbers:
add += 1/i
harmonic_mean = len(numbers)/add
Upvotes: -1
Reputation: 5012
from statistics import harmonic_mean
print(harmonic_mean([1, 4, 4]) # prints 2.0
Upvotes: 1