The Wayback Machine - https://web.archive.org/web/20240607172638/https://www.geeksforgeeks.org/geometric-progression/
Open In App

Geometric Progression

Last Updated : 01 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A sequence of numbers is called a Geometric progression if the ratio of any two consecutive terms is always the same.

In simple terms, A geometric series is a list of numbers where each number, or term, is found by multiplying the previous term by a common ratio r. The general form of Geometric Progression is:

GP-series

Where,
            a = First term
            r = common ratio
            arn-1 = nth term

Example: 

The sequence 2, 4, 8, 16 is a GP because ratio of any two consecutive terms in the series (common difference) is same (4 / 2 = 8 / 4 = 16 / 8 = 2). 

The geometric progression is of two types:

  1. Finite geometric progression
  2. Infinite geometric progression.

1. Finite geometric progression

In finite geometric progression contains a finite number of terms. The last term is always defined in this type of progression. 

Example:

The sequence 1/2,1/4,1/8,1/16,…,1/32768 is a finite geometric series where the first term is 1/2 and the last term is 1/32768.

2. Infinite geometric progression

Infinite geometric progression contains an infinite number of terms.  The last term is not defined in this type of progression.

Example:

Sequence 3, 9, 27, 81, … is an infinite series where the first term is 3 but the last term is not defined.

Fact about Geometric Progression: 

  1. Initial term: In a geometric progression, the first number is called the initial term.
  2. Common ratio: The ratio between a term in the sequence and the term before it is called the “common ratio.” 
  3. The behavior of a geometric sequence depends on the value of the common ratio. If the common ratio is:
    • Positive, the terms will all be the same sign as the initial term.
    • Negative, the terms will alternate between positive and negative.
    • Greater than 1, there will be exponential growth towards positive or negative infinity (depending on the sign of the initial term). 
    • 1, the progression is a constant sequence.
    • Between -1 and 1 but not zero, there will be exponential decay towards zero.
    • -1, the progression is an alternating sequence.
    • Less than -1, for the absolute values there is exponential growth towards (unsigned) infinity, due to the alternating sign.

The formula for the nth term of a Geometric Progression:

If ‘a1′ is the first term and ‘r’ is the common ratio. Thus, the explicit formula for nth term of finite GP series:

 Nth term of a Geometric Progression

The formula for the sum of the nth term of Geometric Progression: 

Sum of the Nth term of Geometric Progression

How do we check whether a series is a Geometric progression or not?

The property of the GP series is that the ratio of the consecutive terms is same. 

Approach: 

  1. First calculate the common ratio r by arr[1] / arr[0]
  2. Iterate over an array and calculate the ratio of the consecutive terms.
  3. Check if the calculated ratio is not equal to the common ratio r 
    • Return false
  4. After traversal, if the calculated ratio is equal to the common ratio r every time 
    • Return true

Below is the implementation of the above approach:

C++




// C++ program to check if a given array
// can form geometric progression
 
#include <bits/stdc++.h>
 
using namespace std;
 
bool is_geometric(int arr[], int n)
 
{
    if (n == 1)
        return true;
 
    // Calculate ratio
    int ratio = arr[1] / (arr[0]);
 
    // Check the ratio of the remaining
    for (int i = 1; i < n; i++) {
        if ((arr[i] / (arr[i - 1])) != ratio) {
            return false;
        }
    }
    return true;
}
 
// Driven Program
int main()
{
    int arr[] = { 2, 6, 18, 54 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    (is_geometric(arr, n)) ? (cout << "True" << endl)
                           : (cout << "False" << endl);
 
    return 0;
}


Java




// Java program to check if a given array
// can form geometric progression
import java.util.Arrays;
 
class GFG {
 
    // function to check series is
    // geometric progression or not
    static boolean is_geometric(int arr[], int n)
    {
        if (n == 1)
            return true;
 
        // Calculate ratio
        int ratio = arr[1] / (arr[0]);
 
        // Check the ratio of the remaining
        for (int i = 1; i < n; i++) {
            if ((arr[i] / (arr[i - 1])) != ratio) {
                return false;
            }
        }
        return true;
    }
 
    // driver code
    public static void main(String[] args)
    {
        int arr[] = { 2, 6, 18, 54 };
        int n = arr.length;
 
        if (is_geometric(arr, n))
            System.out.println("True");
        else
            System.out.println("False");
    }
}


Python3




def is_geometric(li):
    if len(li) <= 1:
        return True
         
    # Calculate ratio
    ratio = li[1]/float(li[0])
     
    # Check the ratio of the remaining
    for i in range(1, len(li)):
        if li[i]/float(li[i-1]) != ratio:
            return False
    return True
 
print(is_geometric([2, 6, 18, 54]))


C#




// C# program to check if a given array
// can form geometric progression
using System;
 
class Geeks {
 
    static bool is_geometric(int[] arr, int n)
    {
        if (n == 1)
            return true;
 
        // Calculate ratio
        int ratio = arr[1] / (arr[0]);
 
        // Check the ratio of the remaining
        for (int i = 1; i < n; i++) {
            if ((arr[i] / (arr[i - 1])) != ratio) {
                return false;
            }
        }
        return true;
    }
 
    // Driven Program
    public static void Main(String[] args)
    {
        int[] arr = new int[] { 2, 6, 18, 54 };
        int n = arr.Length;
 
        if (is_geometric(arr, n))
            Console.WriteLine("True");
        else
            Console.WriteLine("False");
    }
}


PHP




<?php
function is_geometric($arr)
{
    if (sizeof($arr) <= 1)
        return True;
    # Calculate ratio
    $ratio = $arr[1]/$arr[0];
    
    # Check the ratio of the remaining
    for($i=1; $i<sizeof($arr); $i++)
    {
        if (($arr[$i]/($arr[$i-1])) != $ratio)
        {
            return "Not a geometric sequence";
        }
    }       
  return "Geometric  sequence";
}
$my_arr1 = array(2, 6, 18, 54);
 
 
print_r(is_geometric($my_arr1)."\n");
print_r(is_geometric($my_arr2)."\n");
?>


Javascript




<script>
 
// Javascript program to check if a given array
// can form geometric progression
 
// Function to check series is
// geometric progression or not
function is_geometric(arr, n)
{
    if (n == 1)
        return true;
 
    // Calculate ratio
    let ratio = parseInt(arr[1] / (arr[0]));
 
    // Check the ratio of the remaining
    for(let i = 1; i < n; i++)
    {
        if (parseInt((arr[i] /
                     (arr[i - 1]))) != ratio)
        {
            return false;
        }
    }
    return true;
}
 
// Driver code
let arr = [ 2, 6, 18, 54 ];
let n = arr.length;
 
(is_geometric(arr, n)) ?
(document.write("True")) :
(document.write("False"));
 
// This code is contributed by souravmahato348
 
</script>


Output

True

Time Complexity: O(n), Where n is the length of the given array.

Auxiliary Space: O(1)

Basic Program related to Geometric Progression 

More problems related to Geometric Progression 

Recent Articles on Geometric Progression!



Previous Article
Next Article

Similar Reads

Number of GP (Geometric Progression) subsequences of size 3
Given n elements and a ratio r, find the number of G.P. subsequences with length 3. A subsequence is considered GP with length 3 with ration r. Examples: Input : arr[] = {1, 1, 2, 2, 4} r = 2 Output : 4 Explanation: Any of the two 1s can be chosen as the first element, the second element can be any of the two 2s, and the third element of the subseq
18 min read
Minimum number of operations to convert a given sequence into a Geometric Progression
Given a sequence of N elements, only three operations can be performed on any element at most one time. The operations are: Add one to the element.Subtract one from the element.Leave the element unchanged. Perform any one of the operations on all elements in the array. The task is to find the minimum number of operations(addition and subtraction) t
14 min read
Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
Given a binary tree, the task is to check whether the nodes in this tree form an arithmetic progression, geometric progression or harmonic progression.Examples: Input: 4 / \ 2 16 / \ / \ 1 8 64 32 Output: Geometric Progression Explanation: The nodes of the binary tree can be used to form a Geometric Progression as follows - {1, 2, 4, 8, 16, 32, 64}
17 min read
Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and the common ratio is denoted by r. The series looks like this:- [Tex]a, ar, ar^2, ar^3, ar^4,... [/Tex]The task is to find the sum of such a series, mod M. Examples: Input: a = 1, r = 2, N = 10000, M = 10000 Output: 8751 In
7 min read
Sum of elements of a Geometric Progression (GP) in a given range
Given a Geometric Progression series in arr[] and Q queries in the form of [L, R], where L is the left boundary of the range and R is the right boundary. The task is to find the sum of the Geometric Progression elements in the given range. Note: The range is 1-indexed and1 ? L, R ? N, where N is the size of arr.Examples: Input: arr[] = {2, 4, 8, 16
6 min read
Longest subarray forming a Geometric Progression (GP)
Given a sorted array arr[] consisting of distinct numbers, the task is to find the length of the longest subarray that forms a Geometric Progression. Examples: Input: arr[]={1, 2, 4, 7, 14, 28, 56, 89}Output: 4Explanation:The subarrays {1, 2, 4} and {7, 14, 28, 56} forms a GP.Since {7, 14, 28, 56} is the longest, the required output is 4. Input: ar
8 min read
Count subarrays of atleast size 3 forming a Geometric Progression (GP)
Given an array arr[] of N integers, the task is to find the count of all subarrays from the given array of at least size 3 forming a Geometric Progression. Examples: Input: arr[] = {1, 2, 4, 8}Output: 3Explanation: The required subarrays forming geometric progression are: {1, 2, 4}{2, 4, 8}{1, 2, 4, 8} Input: arr[] = {1, 2, 4, 8, 16, 24}Output: 6Ex
6 min read
Sum of an Infinite Geometric Progression ( GP )
Given two integers A and R, representing the first term and the common ratio of a geometric sequence, the task is to find the sum of the infinite geometric series formed by the given first term and the common ratio. Examples: Input: A = 1, R = 0.5Output: 2 Input: A = 1, R = -0.25Output: 0.8 Approach: The given problem can be solved based on the fol
3 min read
Program to find Nth term of given Geometric Progression (GP) series
Given 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 = 4Output: The 4th term of the series is : 16 Input: a = 2 r = 3, N = 5Output: The 5th term of the series is : 162 Approach: To solve the problem follow the below idea: We know
7 min read
Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2
Given an array arr[] consisting of N integers, the following three operations can be performed on any element one at a time: Add one to the element.Subtract one from the element.Leave the element unchanged.The task is to find the minimum cost required to convert it into a Geometric Progression and also find the common ratio. Note: Each addition and
13 min read
Article Tags :
Practice Tags :