JavaScript Program for Sum of n Terms of Arithmetic Progression
Last Updated :
03 Jul, 2024
Arithmetic progression is a sequence of numbers in which the difference between any two consecutive terms is always equal. This difference between any two consecutive terms is known as a common difference and it can be positive, negative, or zero.
Below are the approaches to find Sum of n terms in Arithmetic Progression:
Using Iterative Approach
In this approach, we will use a loop to iterate over each term of A.P. from the first term to the nth term. Inside the loop, we will calculate each term of A.P. using the formula a + i × d, where a is the first term of A.P. i is the index of the current term, and d is the common difference of A.P. and add it to current sum.
Example: The below code implements the iterative approach by using the for loop to find the sum of the AP series.
JavaScript
function sumOfNTermsOfAP(a, d, n) {
let sum = 0;
for (let i = 0; i < n; i++) {
sum += a + i * d;
}
return sum;
}
const firstTerm = 5;
const commonDifference = 3;
const numberOfTerms = 20;
console.log(
"Sum of the first", numberOfTerms,
"terms using Iterative approach ",
sumOfNTermsOfAP
(firstTerm, commonDifference, numberOfTerms));
OutputSum of the first 20 terms using Iterative approach 670
Time Complexity: O(n) , as we are using loop
Space Complexity: O(1) , constant space
Using Recursive Approach
In this approach, we will define a recursive function. This function stops when the number of term equal to 1 , it will return first term of A.P. If n is greater than 1, then recursively call itself to calculate sum of the first n terms.
Example: The below code uses a recursive function which calls itself to fins the sum of n terms AP series.
JavaScript
function sumOfNTermsOfAP(a, d, n) {
if (n === 1) {
return a;
} else {
return a + sumOfNTermsOfAP
(a + d, d, n - 1);
}
}
const firstTerm = 8;
const commonDifference = 6;
const Terms = 15;
console.log(
"Sum of the first", Terms,
"terms using Recursive approach is ",
sumOfNTermsOfAP
(firstTerm, commonDifference, Terms));
OutputSum of the first 15 terms using Recursive approach is 750
Time Complexity: O(n), as function make recursive call n times.
Space Complexity: O(n), n recursive calls are made.
The sum of n terms of A.P. in mathematics can be calculated directly using the formula. In this approch, we will implement the same formula to calculate the sum.
Syntax:
Sn = ((n/2) * (2*a + (n-1) * d) )
where, Sn is sum of n terms of A.P., n is number of terms of A.P., a is first term of given A.P., d is common difference of A.P.
Example: The below code implements the mathematical formula to calculate the sum of the n terms of AP series.
JavaScript
function sumOfNTermsOfAP(n, a, d) {
return ((n / 2) * (2 * a + (n - 1) * d));
}
let n = 10;
let a = 2;
let d = 2;
let sum = sumOfNTermsOfAP(n, a, d);
console.log(
"Sum of first", n,
"terms of AP with first term", a,
"and common difference", d, "is : ", sum);
OutputSum of first 10 terms of AP with first term 2 and common difference 2 is : 110
Time Complexity : O(1) , constant time
Space Complexity : O(1) , constant space
Using three-pointer technique
The three-pointer technique finds common elements in three sorted arrays by initializing pointers, comparing current elements, adding matches to the result, and moving the smallest pointer until all arrays are traversed, ensuring an efficient search.
Example:
JavaScript
function findCommonElements(arr1, arr2, arr3) {
let i = 0, j = 0, k = 0;
const commonElements = [];
while (i < arr1.length && j < arr2.length && k < arr3.length) {
// If the current elements are the same, add to result and move all pointers
if (arr1[i] === arr2[j] && arr2[j] === arr3[k]) {
commonElements.push(arr1[i]);
i++;
j++;
k++;
}
// Move the pointer of the smallest element
else if (arr1[i] < arr2[j]) {
i++;
} else if (arr2[j] < arr3[k]) {
j++;
} else {
k++;
}
}
return commonElements;
}
const arr1 = [1, 5, 10, 20, 40, 80];
const arr2 = [6, 7, 20, 80, 100];
const arr3 = [3, 4, 15, 20, 30, 70, 80, 120];
const common = findCommonElements(arr1, arr2, arr3);
console.log(common);