JavaScript Program to find nth term of Geometric Progression
Last Updated :
03 Apr, 2024
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 be used to find the nth term of G.P.
Given the first term (a), common ratio (r), and an integer N of the Geometric Progression series, the task is to find the Nth term of the series.
Examples:
Input: a = 2 r = 2, N = 4
Output: The 4th term of the series is : 16
Input: a = 2 r = 3, N = 5
Output: The 5th term of the series is : 162
Iterative Approach
In this approach, We initialize the nth term (nthTerm
) with the first term (a
) and multiply it by the common ratio (r
) in each iteration from the second to the nth term, returning the calculated nthTerm
result. This method efficiently finds the nth term of a Geometric Progression (G.P.) without using recursion.
Example: To demonstrate finding nth terms of the G.P. series using an iterative method.
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)
Space Complexity : O(1)
Recursive Approach
In this approach, we define a recursive function that stops (i.e., base case) when n is equal to 1, returning the first term a
. If n is greater than 1, the function recursively calls itself with the same first term a
, common ratio r
, and n – 1 as the term number. It multiplies the result of the recursive call by the common ratio r
and returns the result after the recursive call stops.
Example: To demonstrate finding nth terms of the G.P. series using recursive method.
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 a function that utilizes the nth term formula to calculate the nth terms of a G.P. series and prints 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)
Similar Reads
JavaScript Program for nth Term of Geometric Progression
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
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 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 simple interest
What is âSimple Interestâ?Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments. Simple Interest formula:Simple interest formula is given
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 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
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 to Print 1 to N using Recursion
In this article, we will see how to print 1 to N using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base
2 min read
JavaScript Program to Print N to 1 using Recursion
In this article, we will see how to print N to 1 using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base
1 min read