Skip to content

Home

Sum of powers in range

Calculating the sum of the powers of all the numbers from start to end (both inclusive) can be done using a combination of array methods and JavaScript's exponentiation operator (**).

Using Array.from(), we create an array of the appropriate size. Then, we iterate over all elements, using Array.prototype.reduce(), and calculate the sum of their powers. In order to get the value of each element, we add the start value to the current index i, then use the exponentiation operator (**) to raise it to the power.

Omitting the power argument will default to a power of 2, and omitting the start argument will default to a starting value of 1.

const sumPower = (end, power = 2, start = 1) =>
  Array.from({ length: end + 1 - start }).reduce(
    (acc, _, i) => acc + (i + start) ** power, 0
  );

sumPower(10); // 385
sumPower(10, 3); // 3025
sumPower(10, 3, 5); // 2925
💬 Note

If you're working with older browsers or environments that don't support the exponentiation operator (**), you can use Math.pow() instead.

More like this

Start typing a keyphrase to see matching snippets.