Open In App

JavaScript Program for Sum of n Terms of Arithmetic Progression

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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));

Output
Sum 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));

Output
Sum 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.

Using Direct Formula

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);

Output
Sum 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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads