Skip to content

Commit f84d413

Browse files
authored
Added quadratic mean
Added a quadratic mean of the given numbers, sqrt ((n1^2+n2^2+...+nk^2)/k).
1 parent 24c2bea commit f84d413

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/main/java/com/thealgorithms/maths/Means.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ public static Double harmonic(final Iterable<Double> numbers) {
107107
return size / sumOfReciprocals;
108108
}
109109

110+
public static Double quadratic(final Iterable<Double> numbers) {
111+
checkIfNotEmpty(numbers);
112+
double sum = StreamSupport.stream(numbers.spliterator(), false).reduce(0d, (x, y) -> x * x + y * y);
113+
int size = IterableUtils.size(numbers);
114+
return Math.sqrt(sum / size);
115+
}
116+
110117
/**
111118
* Validates that the input iterable is not empty.
112119
*

0 commit comments

Comments
 (0)