Sum of Square of First N Natural Numbers using Mathematical Formula
This approach uses the direct mathematical formula (n * (n + 1) * (2 * n + 1)) / 6 to calculate the sum of the squares of the first n natural numbers.
Syntax:
Sum = (n * (n + 1) * (2 * n + 1)) / 6;
Example: This example uses the mathematical formula to get a sum of squares of first n natural numbers in JavaScript.
let n = 8;
let sum = (n * (n + 1) * (2 * n + 1)) / 6;
console.log(sum);
Output
204
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 N Natural Numbers using for Loop
- Sum of Square of First N Natural Numbers using reduce() Method
- Sum of Square of First N Natural Numbers using Mathematical Formula
- Sum of Square of First N Natural Numbers using Recursion