878751. Geometric Progression Term
Easy
Geometric Progression Term
Summary
Find the term at a given position in a geometric progression. The geometric progression is defined by an initial value and a common ratio.
Detailed Explanation
To solve this problem, we can use the formula for a geometric progression: term = a * r^(n-1), where a is the initial value, r is the common ratio, and n is the position of the term. We need to calculate the term at a given position n.
Here's the step-by-step breakdown:
- Calculate the term at position
n:term = a * r^(n-1) - Return the calculated term
Time complexity: O(1) since we're performing constant-time operations Space complexity: O(1) since we're not using any extra space that scales with the input size
Optimized Solutions
Java
public class GeometricProgression {
public static double getTerm(double a, double r, int n) {
return a * Math.pow(r, n - 1);
}
}