Suppose you are given a list L of n numbers and an integer k. Is there an efficient way to calculate the sum of all products of
L
n
k. Is there an efficient way to calculate the sum of all products of
For k=2,
let's s = SUM_x_in_L x (sum of the numbers) and sq = SUM_x_in_L x^2 (sum of the squares)
s = SUM_x_in_L x
sq = SUM_x_in_L x^2
then it's SUM_x_in_L (s - x) * x / 2 = (s * s - sq) / 2
SUM_x_in_L (s - x) * x / 2 = (s * s - sq) / 2