![]() |
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 Table of Content Iterative ApproachIn this approach, We initialize the nth term ( Example: To demonstrate finding nth terms of the G.P. series using an iterative method.
Output The 7 th term of the geometric progression is 192 Time Complexity : O(n) Space Complexity : O(1) Recursive ApproachIn this approach, we define a recursive function that stops (i.e., base case) when n is equal to 1, returning the first term Example: To demonstrate finding nth terms of the G.P. series using recursive method.
Output The 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. Direct Formula ApproachIn 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) 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.
Output The 6th term of the G.P. is: 128 Time Complexity: O(logn), we are using power inbuilt function Space Complexity: O(1) |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |