Skip to content

Home

Sum of powers in range

Calculates the sum of the powers of all the numbers from start to end (both inclusive).

const sumPower = (end, power = 2, start = 1) =>
  Array(end + 1 - start)
    .fill(0)
    .map((x, i) => (i + start) ** power)
    .reduce((a, b) => a + b, 0);

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

More like this

Start typing a keyphrase to see matching snippets.