Series GP | Practice | GeeksforGeeks

Given the A and R i,e first term and common ratio of a GP series. Find the Nth term of the series.
Note: As the answer can be rather large print its modulo 1000000007 (109 + 7).
Example 1:
Input: A = 2, R = 2, N = 4
Output: 16
Explanation: The GP series is 
2, 4, 8, 16, 32,... in which 16 
is th 4th term.

Example 2:
Input: A = 4, R = 3, N = 3
Output: 36
Explanation: The GP series is
4, 12, 36, 108,.. in which 36 is
the 3rd term.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function Nth_term() which takes A, R and N as input parameter ans returns Nth term of the GP series modulo 109 + 7.
 
Expected Time Complexity: O(log2(N))
Expected Space Complexity: O(1)

Constraints:
1 <= A, R, N <= 1000000
+6
jaideepnagillscsebtech1 month ago
This question is total dog shit
 
-4
parsaniyayash71 month ago
class Solution{   public int Nth_term(int a, int r, int n)   {       // code here       int mod= 1000000007;      int S = a * ((int)Math.pow(r,n-1))%mod;          return S;   }   } 
0
bhaskor882 months ago
Can anyone please help me out with the issue in the below code block ?
class Solution
{
    public int Nth_term(int a, int r, int n)
    {
        // code here
        int modulo = 1000000007;
        return (a*power(r, n-1))%modulo;
    }
    
    // Find power of a number in O(log n) time complesity
    private int power(int base, int exponent){
        if(exponent == 0){
            return 1;   
        }
        
        int temp = power(base, exponent/2);
        
        if(exponent%2 == 0)
            return temp * temp;
        else
            return base * temp * temp;
    }
}
0
hrshuva92 months ago
concept:let (a, r, n) = (2, 3, 4)res = a * r^n-1n = 0, 1, 2, 3, 42 X 3^0 = 22 X 3^1 = 62 X 3^2 = 182 X 3^3 = 54     ------- result is for n-12 X 3^4 = 162
0
2017bcs0982 months ago
// c++ code
// also refer: https://cp-algorithms.com/algebra/binary-exp.html
long long binpow(long long a, long long b, long long m) {
        a %= m;
        long long res = 1;
        while (b > 0) {
            if (b & 1)
                res = res * a % m;
            a = a * a % m;
            b >>= 1;
        }
        return res;
    }

   	int Nth_term(int a, int r, int n){
   	    const int mod = 1e9 + 7;
   	    return a * binpow(r, n-1, mod) % mod;
   	}
0
nidhikumaridbg14 months ago
//it is giving compilation error
//what is wrong 
//if anybody understands please tell me
 public:   int Nth_term(int a, int r, int n){    //gp nth term=a*pow(r,n-1)    //modular arithmetics    //(a*pow(r,n-1))%(pow(10,9)+7)    int p=pow(10,9)+7;    int gp=(a*pow(r,n-1))%p;        return gp;           }    
0
naharnikhil6
This comment was deleted.
0
naharnikhil6
This comment was deleted.
+3
ashutoshkumarsme188 months ago
class Solution:def Nth_term(self, a, r, n):    value = a*pow(r,i)       return value # Code here
#{ #  Driver Code Starts#Initial Template for Python 3
if __name__ == '__main__':T=int(input())for i in range(T): a, r, n = input().split() a = int(a) r = int(r) n = int(n) ob = Solution() ans = ob.Nth_term(a, r, n) print(ans)# } Driver Code Ends
 
 
This is the write code and it shows one test case does not pass that is because the test case answer is wrong
0
Siddhartha Gupta8 months ago
Siddhartha Gupta
This is my solution in Javascript, doesn't work with a large number. class Solution {
    Nth_term(a,r,n){        //code herevalue = a*pow(r,n-1)// # for i in range(0,n)://             # value =         var something = Math.pow(r,n-1)        // console.log("something ", something)        var result = a*something;        var limited = Math.pow(10,9)+7        // console.log("limited ", limited)        // console.log("result ", result)        if(result!= Infinity) {            return result % limited        }
}}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.