Nth Term of Geometric Progression in Java
There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth term that needs to be calculated.
Example1
Input
int num1 = 2, // first term
int num2 = 2 // common ratio
int num3 = 4 // 4th term to be found
Output: The fourth term is 16.
Explanation: To calculate the following term in a geometric progression, multiply the current term by the common ratio. Therefore, the second term will be as follows if the first term is the present term:
secondTerm = a1 x a2 = 2 x 2 = 4, using the second term we can compute the third term, and so on.
thirdTerm = secondTerm x common ratio = 4 x 2 = 8
fourthTerm = thirdTerm x common ratio = 8 x 2 = 16
Thus, we get the fourth term as 16.
Example2
Input
int num1 = 3, // first term
int num2 = 3 // common ratio
int num3 = 3 //third term to be found
Output: The third term is 27.
Brute Force Approach
The idea is to utilise the formula axe r to calculate the nth term of the geometric progression (n - 1). The magnitude of r (n - 1)
Nth Term of Geometric Progression Program in Java
public class NthtermGP1
{
public long nth_term_of_GP(int num, int first_term, int common_ratio)
{
long ans = (first_term * power(common_ratio, num - 1)) ;
return ans;
}
public long power(int common_ratio, int num)
{
if(num == 0)
{
return 1;
}
int j1 = 0;
long power = 1;
while(j1 < num)
{
power = (power * common_ratio);
j1 = j1 + 1;
}
return power;
}
public static void main(String argvs[])
{
int first_term = 2;
int common_ratio = 2;
int nth_term = 4;
NthtermGP1 object1 = new NthtermGP1();
long nth_term1 = object1.nth_term_of_GP(nth_term, first_term, common_ratio);
System.out.print("For a geometric progression which has the ");
System.out.print("first term as: " + first_term + " and the common ratio as: " + common_ratio);
System.out.print(", the " + nth_term + "th term is: " + nth_term1);
System.out.println( "\n" );
first_term = 3;
common_ratio = 3;
nth_term = 3;
nth_term1 = object1.nth_term_of_GP(nth_term, first_term, common_ratio);
System.out.print("For a geometric progression which has the ");
System.out.print("first term as: " + first_term + " and the common ratio as: " + common_ratio);
System.out.print(", the " + nth_term + "rd term is: " + nth_term1);
}
}
Output
Complexity analysis: Because r(n - 1) is calculated using a while loop, the program's time complexity is O(n), where n is the number that needs to be calculated. Since the programme uses no more space, its space complexity is O(1).
We can use additional optimization to speed up the process of computing the value of r. (n - 1).
Recursive Methodology
NthtermGP2 .java
public class NthtermGP2
{
public long nth_term_of_GP(int num, int first_term, int common_ratio)
{
long ans = (first_term * power(common_ratio, num - 1)) ;
return ans;
}
public long power(int common_ratio, int num)
{
if(num == 0)
{
return 1;
}
long temp = power(common_ratio, num / 2);
if(num % 2 == 0)
{
return (temp * temp);
}
else
{
return ((temp * temp) * common_ratio);
}
}
public static void main(String argvs[])
{
int first_term = 2;
int common_ratio = 2;
int nth_term = 4;
NthtermGP2 object1 = new NthtermGP2();
long nth_term1 = object1.nth_term_of_GP(nth_term, first_term, common_ratio);
System.out.print("For a geometric progression which has the ");
System.out.print("first term as: " + first_term + " and the common ratio as: " + common_ratio);
System.out.print(", the " + nth_term + "th term is: " + nth_term1);
System.out.println( "\n" );
first_term = 3;
common_ratio = 3;
nth_term = 3;
nth_term1 = object1.nth_term_of_GP(nth_term, first_term, common_ratio);
System.out.print("For a geometric progression which has the ");
System.out.print("first term as: " + first_term + " and the common ratio as: " + common_ratio);
System.out.print(", the " + nth_term + "rd term is: " + nth_term1);
}
}
Output
NthtermGP3.java
import java.io.*;
import java.lang.*;
class NthtermGP3 {
public static int NthTermOfGP(int num, int ratio, int N1)
{
return (num * (int)(Math.pow(ratio, N1 - 1)));
}
public static void main(String[] args)
{
int num = 2;
int ratio = 2;
int N1 = 4;
System.out.print("The " + N1 + "th term of the"
+ " series is : "
+ NthTermOfGP(num, ratio, N1));
}
}
Output