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);
Similar Reads
JavaScript Program for Sum of n terms of Geometric Progression
Geometric Progression is a sequence of numbers whose next term is calculated by multiplying the current term with a fixed number known as a common ratio. We are going to learn how we can find the sum of n in terms of Geometric Progression in different ways using JavaScript including iteration, recur
3 min read
JavaScript Program for Sum of n Terms of Harmonic Progression
One can find the sum of n terms of Harmonic Progression using JavaScript. Harmonic Progression is a sequence of real numbers in which each term is reciprocal of Arithmetic Progression. There are different approaches to finding the sum of n terms of Harmonic Progression which are discussed below: Tab
3 min read
JavaScript Program for Sum of Infinite Terms in Geometric Progression
Geometric Progression is a sequence of numbers whose next term is calculated by multiplying the current term with a fixed number known as the common ratio. Below are the approaches to find the sum of infinite terms in a Geometric progression using JavaScript: Table of Content Using Iterative Approac
3 min read
Arithmetic Progression â Sum of First n Terms | Class 10 Maths
Arithmetic Progression is a sequence of numbers where the difference between any two successive numbers is constant. For example 1, 3, 5, 7, 9â¦â¦. is in a series which has a common difference (3 â 1) between two successive terms is equal to 2. If we take natural numbers as an example of series 1, 2,
8 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
Arithmetic Progressions Class 10- NCERT Notes
Arithmetic Progressions (AP) are fundamental sequences in mathematics where each term after the first is obtained by adding a constant difference to the previous term. Understanding APs is crucial for solving problems related to sequences and series in Class 10 Mathematics. These notes cover the ess
7 min read
How to find the common difference of an Arithmetic Progression whose sum is given?
A sequence is defined as a list of items or objects that are arranged in a sequential manner. It is also defined as the set of numbers in a defined order which follows some rule. Suppose if a1, a2, a3, a4... etc represent the terms of a given sequence, then the subscripts 1, 2, 3, 4... represent the
4 min read
How to Find the Common Difference of an Arithmetic Progression?
How to find the common difference of an Arithmetic Progression? A sequence is also known as a progression and is defined as the successive arrangement of numbers in order while following some specific rules. Depending upon the set of rules followed by a sequence, it is classified into various kinds,
6 min read
Arithmetic Progression in Maths
Arithmetic Progression (AP) or Arithmetic Sequence is simply a sequence of numbers such that the difference between any two consecutive terms is constant.Some Real World Examples of APNatural Numbers: 1, 2, 3, 4, 5, . . . with a common difference 1Even Numbers: 2, 4, 6, 8, 10, . . . with a common di
3 min read
Arithmetic Progressions Class 10 Maths Notes Chapter 5
CBSE Class 10 Maths Notes Chapter 4 Arithmetic Progressions are an outstanding resource created by our team of knowledgeable Subject Experts at GfG. As ardent supporters of students' education, we place a high priority on their learning and development, which is why we have written these in-depth no
13 min read