Open In App

Cube Sum of First n Natural Numbers in JavaScript

Last Updated : 16 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To find the cube sum, we can iterate over each natural number from 1 to ‘n’. For each number, we will cube it (multiply it by itself twice) and add the result to a running total. This means we are going to add up the cubes of each number from 1 to ‘n’.

There are different methods to cube the sum of the first n natural numbers in JavaScript which are as follows:

Using a for-loop

The for loop is used to iterate through each natural number from 1 to ‘n’ inclusive, where ‘n’ is the parameter passed to the function. After processing all natural numbers from 1 to ‘n’ in the loop, the function cubeSum returns the final value of sum, which represents the total sum of the cubes of the first ‘n’ natural numbers.

Example: Iteratively computing the sum of cubes for numbers from 1 to N using a for loop in JavaScript.

Javascript




function cubeSum(n) {
      let sum = 0;
      for (let i = 1; i <= n; i++) {
        sum += i ** 3;
     }
      return sum;
}
 
console.log(cubeSum(5));


Output

225

Using recursion

Recursion is used to calculate the cube sum of the first ‘n’ natural numbers. By using recursion, the function effectively breaks down the problem of finding the cube sum of ‘n’ natural numbers into smaller subproblems, making the code concise and elegant.

Example: Recursively calculating the sum of cubes for numbers from 1 to N using a JavaScript function.

Javascript




function cubeSum(n) {
      if (n === 0) {
        return 0;
      } else {
        return n ** 3 + cubeSum(n - 1);
      }
}
 
console.log(cubeSum(4));


Output

100

Using the formula for the sum of cubes

A formula for the sum of cubes of the first ‘n’ natural numbers is used to compute the result directly. The formula is (n * (n + 1) / 2) ^ 2. This formula provides a direct and efficient way to calculate the cube sum. This approach provides a more efficient way to compute the cube sum without iterating through each natural number individually.

Example: Computing the sum of cubes for numbers from 1 to N using a closed-form mathematical formula in JavaScript

Javascript




function cubeSum(n) {
    return ((n * (n + 1)) / 2) ** 2;
  }
   
  console.log(cubeSum(6));


Output

441

Using the reduce() method

The reduce method is used to iterate over an array of the first n natural numbers and calculate the cube sum. We will first create an array of length n using Array.from(), and then use the reduce() method to calculate the sum of cubes. The initial value of the sum is set to 0, and for each number in the array, we add its cube to the sum. Finally, the computed sum is returned as the result.

Example: Calculating the sum of cubes for numbers from 1 to N using array generation and reduction in JavaScript.

Javascript




const cubeSum = (n) =>
      Array.from({ length: n }, (_, i) => i + 1).reduce(
        (sum, num) => sum + num ** 3,0);
 
console.log(cubeSum(7));


Output

784


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads