Cube Sum of First n Natural Numbers in JavaScript
Last Updated :
16 Feb, 2024
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));
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));
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));
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));
Similar Reads
Sum of Squares of First N Natural Numbers in JavaScript The sum of squares of the first n natural numbers is a mathematical problem of finding the sum of squares of each number up to a given positive number n. We can find the sum of squares of first n natural numbers in JavaScript using the approaches listed below.Table of Content Sum of Square of First
3 min read
Sum of First n Natural Numbers Sum of n Natural Numbers is simply an addition of 'n' numbers of terms that are organized in a series, with the first term being 1, and n being the number of terms together with the nth term. The numbers that begin at 1 and terminate at infinity are known as natural numbers. Sum of the first n natur
6 min read
JavaScript Program to Find All Factors of a Natural Number Finding all factors of a natural number is a common task in programming and mathematics. A factor of a number is an integer that can be multiplied by another integer to produce the original number. In this article, we will discuss various approaches to finding all factors of a natural number using J
2 min read
Find the sum of the first 80 natural numbers Numerals are the mathematical figures used in financial, professional as well as a social field in the social world. The digits and place value in the number and the base of the number system determine the value of a number. Numbers are used in various mathematical operations such as summation, subt
6 min read
JavaScript - Factorial of a number in JS The factorial of a non-negative integer is the product of all positive integers less than or equal to that number. Itâs denoted by ân!â where n is the integer. Here are the various methods to find the factorial of a number in JavaScript.Logicx!= 1*(x-3)*(x-2)*(x-1).......xNote:Â Factorials of negativ
3 min read
JavaScript Program for Nth Catlan Numbers Catalan numbers are defined as a mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations. The nth term in the sequence denoted Cn, is found in the following formula: The first few Catalan numbers for n = 0, 1, 2, 3, â¦
3 min read
JavaScript Program to Find the Sum of Natural Numbers using Recursion Finding the sum of natural numbers using recursion involves defining a function that recursively adds numbers from 1 to the given limit. The function repeatedly calls itself with decreasing values until reaching the base case, where it returns the sum. Example: Input: 5Output: 15Explanation: 1 + 2 +
1 min read
Sum of Cube of N Natural Numbers: Formula, Proof and Examples Sum of cube of n natural numbers is a mathematical pattern on which various questions were asked in competitive exam. So, the sum of cube of n natural numbers is obtained by the formula [n2(n+1)2]/4 where S is sum and n is number of natural numbers. Natural Numbers are the numbers started from 1 and
6 min read
Check Whether a Number is Strong Number in JavaScript Strong Numbers are the numbers whose sum of the factorial of digits is equal to the original number. In JavaScript, we can check whether the number is a strong number or not using various approaches. In this article, we will explore all the possible approaches with their practical implementation. Th
3 min read
Sum of Squares of Odd Numbers in an Array in JavaScript The task involves iterating through the array, identifying odd numbers, squaring them, and summing up the squares. The different approaches to accomplish this task are listed below.Table of ContentIterative ApproachUsing Array MethodsUsing map and reduce MethodsIterative ApproachIn this approach, we
3 min read