JavaScript Program for nth Term of Geometric Progression
Last Updated :
15 Mar, 2024
In this article, we are going to learn how to find the nth term of Geometric Progression using JavaScript. We will see different approaches that will return the nth term of G.P. 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 different approaches to finding the nth term of G.P. in Javascript:
Iterative Approach
In this approach, we define an iterative function. We initialize nthTerm as a variable with the value of the first term a. We will traverse the loop from second term to n term and in each iteration, multiply term by the common ratio r. After loop completes we will return nth term of G.P.
Example: To demonstrate finding nth terms of the G.P. series using iterative function which uses loop to traverse from 2 until n reaches to print the result.
JavaScript
function nthTermOfGPIterative(a, r, n) {
let nthterm = a;
for (let i = 2; i <= n; i++) {
nthterm *= r;
}
return nthterm;
}
const a = 3;
const r = 2;
const n = 7;
const term = nthTermOfGPIterative(a, r, n);
console.log("The", n, "th term of the geometric progression is ", term);
OutputThe 7 th term of the geometric progression is 192
Time Complexity : O(n), we are loop
Space Complexity : O(1) , constant space
Recursive Approach
In this approach we will define a recursive function. This function stops( i.e. base case) when n is equal to 1 it return first term i.e. a . If n is greater than 1, recursively call the function with the same first term a, common ratio r, and n - 1 as the term number. Multiply the result of the recursive call by the common ratio r. Return the result after recursive call stops.
Example: To demonstrate finding nth terms of the G.P. series using recursive function which uses recursive calls till base case reaches to print the result.
JavaScript
function nthTermOfGPRecursive(a, r, n) {
if (n === 1) {
return a;
}
return r * nthTermOfGPRecursive(a, r, n - 1);
}
const a = 3;
const r = 2;
const n = 7;
const term = nthTermOfGPRecursive(a, r, n);
console.log(
"The", n,
"th term of the geometric progression using recursive function is ", term);
OutputThe 7 th term of the geometric progression using recursive function is 192
Time Complexity : O(n) , as function make recursive call n times.
Space Complexity : O(n) , n recursive calls are made.
In this approach we will use direct formula to find nth term of G.P. The formula for calculating nth term of G.P. is an = a1 × r(n-1) . We will define the function which will return the nth term of G.P. by using its formula.
Syntax:
an = a1 × r(n-1)
Parameters:
- an: It represents the nth term of G.P.
- a1: It is the first term of G.P.
- r : It is the common ration of G.P.
- n: It is the number of term which we are calculating.
Example: To demonstrate finding nth terms of the G.P. series using the function which uses nth term formula to find nth terms of an G.P. series to print the result.
JavaScript
function nthTermGP(firstTerm, commonRatio, n) {
return firstTerm * Math
.pow(commonRatio, n - 1);
}
const firstTerm = 4;
const commonRatio = 2;
const n = 6;
const nthTerm = nthTermGP(firstTerm, commonRatio, n);
console.log("The", n + "th term of the G.P. is:", nthTerm);
OutputThe 6th term of the G.P. is: 128
Time Complexity : O(logn), we are using power inbuilt function
Space Complexity : O(1) , constant space
Similar Reads
JavaScript Program to find nth term of Geometric Progression
Geometric Progression is a sequence of numbers in which each term is obtained by multiplying the previous term by a constant number called the common ratio. In this article, we will learn how to find the nth term of Geometric Progression using JavaScript. We will explore various approaches that can
4 min read
JavaScript Program to find nth term of Arithmetic Progression
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. The nth term of A.P. in mathematics is calculated by
2 min read
JavaScript Program to Find Geometric mean in G.P.
The geometric mean in a geometric progression (G.P.) is computed as the nth root of the product of all terms, where n is the number of terms. It provides a measure of central tendency in exponential sequences. This calculation is crucial in various mathematical and statistical analyses and we will s
2 min read
JavaScript Program for Binary Representation of Next Number
Given a binary input that represents a binary representation of the positive number n, find a binary representation of n+1. We will try to find out this with the different approaches discussed below. Examples: Input : 10011 Output : 10100 Here n = (19)10 = (10011)2 next greater integer = (20)10 = (1
3 min read
JavaScript Program to Find the Next Power of Two
Given a positive integer, find the next power of two greater than the given number using JavaScript. Examples: Input: N = 6Output: 8Explanation: The next power of two greater than 6 is 8.Input: N = 16Output: 16Explanation: 16 is already a power of two, so the next power of two is also 16.Below are t
3 min read
JavaScript Program to Find Factorial of a Number using Recursion
In this article, we are going to learn about finding a Factorial of a Number using Recursion. Calculating the factorial of a number using recursion means implementing a function that calls itself to find the factorial of a given number. The factorial of a non-negative integer "n" is the product of a
2 min read
JavaScript Program to Compute Iterative Power of a Number
This article will demonstrate how to compute the iterative power of a number using JavaScript. It will include iterative methods to calculate and display the power of a given number. Methods to Compute the Iterative Power of a NumberTable of Content Method 1: Using JavaScript for LoopMethod 2: Using
3 min read
JavaScript Program for Sum of Digits of a Number using Recursion
We are given a number as input and we have to find the sum of all the digits contained by it. We will split the digits of the number and add them together using recursion in JavaScript. Table of Content Recursively Summing DigitsUsing string manipulation with recursionRecursively Summing DigitsIn th
2 min read
JavaScript Program to Compute Power of a Number
In this article, we will demonstrate different approaches for computing the Power of a Number using JavaScript. The Power of a Number can be computed by raising a number to a certain exponent. It can be denoted using a^b, where the 'a' represents the base number & the 'b' represents the number t
3 min read
JavaScript Program to Find G.C.D Using Recursion
The greatest common divisor (G.C.D) of two integers is the largest positive integer that divides both numbers without leaving a remainder. In JavaScript, finding the greatest common divisor (G.C.D) of two numbers using recursion is a common programming task. Recursion is a powerful technique where a
4 min read