Posted on by Kalkicode
Code Mathematics

Find N-th element in geometric progression series

A geometric progression (GP) is a sequence of numbers in which each term is obtained by multiplying the previous term by a fixed, non-zero number called the common ratio. Finding the N-th element in a geometric progression is a common mathematical operation that involves determining the value of a specific term at a given position in the sequence. This article aims to explain how to find the N-th element in a geometric progression using a C program.

Problem Statement

Given the first term 'a', common ratio 'r', and the position of the term 'n', the problem is to find the value of the N-th element in the geometric progression.

Example

For instance, consider a geometric progression with a first term of 3, a common ratio of 2, and the position 'n' as 6. The N-th element in this series is 96. Similarly, for a series with a first term of 2, a common ratio of 3, and 'n' as 7, the N-th element is 1458. For a series with a first term of 4.3, a common ratio of 3, and 'n' as 3, the N-th element is 38.7.

Idea to Solve

To solve this problem, we can use the formula to find the N-th element in a geometric progression:

N-th element = a * (ratio ^ (n - 1))

Where:

  • 'a' is the first term of the series
  • 'ratio' is the common ratio
  • 'n' is the position of the term

Pseudocode

function nth_gp_element(a, ratio, n):
    Print "[ Start :", a, ", Ratio :", ratio, ", N-th :", n, " ]"
    Calculate the N-th element using the formula
    Print "Result :", result

function main():
    nth_gp_element(3, 2, 6)
    nth_gp_element(2, 3, 7)
    nth_gp_element(4.3, 3, 3)

Algorithm Explanation

  1. The nth_gp_element function takes three parameters: 'a', 'ratio', and 'n'.
  2. It prints the given values of 'a', 'ratio', and 'n' as information.
  3. It calculates the N-th element using the formula result = a * (pow(ratio, n - 1)).
  4. It prints the calculated N-th element.
  5. The main function tests the nth_gp_element function with different values.

Code Solution

// C program 
// Find N-th element in geometric progression series
#include <stdio.h>

#include <math.h>

// Find nth term of geometric progression
// a : starting point
// ratio : common ratio
// n : nth element
void nth_gp_element(double a, double ratio, int n)
{
    printf("\n [ Start : %lf, Ratio : %lf, N-th : %d ] ", a, ratio, n);
    //Find nth element in GP
    double result = a * (pow(ratio, n - 1));
    printf("\n Result : %lf", result);
}
// Driver code 
int main()
{
    // Test Cases
    nth_gp_element(3, 2, 6);
    nth_gp_element(2, 3, 7);
    nth_gp_element(4.3, 3, 3);
    return 0;
}

Output

 [ Start : 3.000000, Ratio : 2.000000, N-th : 6 ]
 Result : 96.000000
 [ Start : 2.000000, Ratio : 3.000000, N-th : 7 ]
 Result : 1458.000000
 [ Start : 4.300000, Ratio : 3.000000, N-th : 3 ]
 Result : 38.700000
// Java program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	public void nth_gp_element(double a, double ratio, int n)
	{
		System.out.print("\n [ Start : " + a + ", Ratio : " + ratio + ", N-th : " + n + " ] ");
		//Find nth element in GP
		double result = a * (Math.pow(ratio, n - 1));
		System.out.print("\n Result : " + result);
	}
	public static void main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.nth_gp_element(3, 2, 6);
		obj.nth_gp_element(2, 3, 7);
		obj.nth_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 3.0, Ratio : 2.0, N-th : 6 ]
 Result : 96.0
 [ Start : 2.0, Ratio : 3.0, N-th : 7 ]
 Result : 1458.0
 [ Start : 4.3, Ratio : 3.0, N-th : 3 ]
 Result : 38.699999999999996
//Include header file
#include <iostream>
#include<math.h>
using namespace std;

// C++ program
// Find N-th element in geometric progression series

class MyMath
{
	public:
		// Find nth term of geometric progression
		// a : starting point
		// ratio : common ratio
		// n : nth element
		void nth_gp_element(double a, double ratio, int n)
		{
			cout << "\n [ Start : " << a << ", Ratio : " << ratio << ", N-th : " << n << " ] ";
			//Find nth element in GP
			double result = a * (pow(ratio, n - 1));
			cout << "\n Result : " << result;
		}
};
int main()
{
	MyMath obj = MyMath();
	// Test Cases
	obj.nth_gp_element(3, 2, 6);
	obj.nth_gp_element(2, 3, 7);
	obj.nth_gp_element(4.3, 3, 3);
	return 0;
}

Output

 [ Start : 3, Ratio : 2, N-th : 6 ]
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ]
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ]
 Result : 38.7
//Include namespace system
using System;
// C# program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	public void nth_gp_element(double a, double ratio, int n)
	{
		Console.Write("\n [ Start : " + a + ", Ratio : " + ratio + ", N-th : " + n + " ] ");
		//Find nth element in GP
		double result = a * (Math.Pow(ratio, n - 1));
		Console.Write("\n Result : " + result);
	}
	public static void Main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.nth_gp_element(3, 2, 6);
		obj.nth_gp_element(2, 3, 7);
		obj.nth_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 3, Ratio : 2, N-th : 6 ]
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ]
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ]
 Result : 38.7
<?php
// Php program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	public	function nth_gp_element($a, $ratio, $n)
	{
		echo "\n [ Start : ". $a .", Ratio : ". $ratio .", N-th : ". $n ." ] ";
		//Find nth element in GP
		$result = $a * (pow($ratio, $n - 1));
		echo "\n Result : ". $result;
	}
}

function main()
{
	$obj = new MyMath();
	// Test Cases
	$obj->nth_gp_element(3, 2, 6);
	$obj->nth_gp_element(2, 3, 7);
	$obj->nth_gp_element(4.3, 3, 3);
}
main();

Output

 [ Start : 3, Ratio : 2, N-th : 6 ]
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ]
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ]
 Result : 38.7
// Node Js program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	nth_gp_element(a, ratio, n)
	{
		process.stdout.write("\n [ Start : " + a + ", Ratio : " + ratio + ", N-th : " + n + " ] ");
		//Find nth element in GP
		var result = a * (Math.pow(ratio, n - 1));
		process.stdout.write("\n Result : " + result);
	}
}

function main()
{
	var obj = new MyMath();
	// Test Cases
	obj.nth_gp_element(3, 2, 6);
	obj.nth_gp_element(2, 3, 7);
	obj.nth_gp_element(4.3, 3, 3);
}
main();

Output

 [ Start : 3, Ratio : 2, N-th : 6 ]
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ]
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ]
 Result : 38.699999999999996
#  Python 3 program
#  Find N-th element in geometric progression series

class MyMath :
	#  Find nth term of geometric progression
	#  a : starting point
	#  ratio : common ratio
	#  n : nth element
	def nth_gp_element(self, a, ratio, n) :
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N-th : ", n ," ] ", end = "")
		# Find nth element in GP
		result = a * (ratio**(n - 1))
		print("\n Result : ", result, end = "")
	

def main() :
	obj = MyMath()
	#  Test Cases
	obj.nth_gp_element(3, 2, 6)
	obj.nth_gp_element(2, 3, 7)
	obj.nth_gp_element(4.3, 3, 3)

if __name__ == "__main__": main()

Output

 [ Start :  3 , Ratio :  2 , N-th :  6  ]
 Result :  96
 [ Start :  2 , Ratio :  3 , N-th :  7  ]
 Result :  1458
 [ Start :  4.3 , Ratio :  3 , N-th :  3  ]
 Result :  38.699999999999996
#  Ruby program
#  Find N-th element in geometric progression series

class MyMath

	#  Find nth term of geometric progression
	#  a : starting point
	#  ratio : common ratio
	#  n : nth element
	def nth_gp_element(a, ratio, n)
	
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N-th : ", n ," ] ")
		# Find nth element in GP
		result = a * (ratio**(n - 1))
		print("\n Result : ", result)
	end
end
def main()

	obj = MyMath.new()
	#  Test Cases
	obj.nth_gp_element(3, 2, 6)
	obj.nth_gp_element(2, 3, 7)
	obj.nth_gp_element(4.3, 3, 3)
end
main()

Output

 [ Start : 3, Ratio : 2, N-th : 6 ] 
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ] 
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ] 
 Result : 38.699999999999996
// Scala program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	def nth_gp_element(a: Double, ratio: Double, n: Int): Unit = {
		print("\n [ Start : " + a + ", Ratio : " + ratio + ", N-th : " + n + " ] ");
		//Find nth element in GP
		var result: Double = a * (Math.pow(ratio, n - 1));
		print("\n Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMath = new MyMath();
		// Test Cases
		obj.nth_gp_element(3, 2, 6);
		obj.nth_gp_element(2, 3, 7);
		obj.nth_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 3.0, Ratio : 2.0, N-th : 6 ]
 Result : 96.0
 [ Start : 2.0, Ratio : 3.0, N-th : 7 ]
 Result : 1458.0
 [ Start : 4.3, Ratio : 3.0, N-th : 3 ]
 Result : 38.699999999999996
import Foundation

// Swift program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	func nth_gp_element(_ a: Double, _ ratio: Double, _ n: Int)
	{
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N-th : ", n ," ] ", terminator: "");
		//Find nth element in GP
		let result: Double = a * (pow(ratio, Double(n - 1)));
		print("\n Result : ", result, terminator: "");
	}
}
func main()
{
	let obj: MyMath = MyMath();
	// Test Cases
	obj.nth_gp_element(3, 2, 6);
	obj.nth_gp_element(2, 3, 7);
	obj.nth_gp_element(4.3, 3, 3);
}
main();

Output

 [ Start :  3.0 , Ratio :  2.0 , N-th :  6  ]
 Result :  96.0
 [ Start :  2.0 , Ratio :  3.0 , N-th :  7  ]
 Result :  1458.0
 [ Start :  4.3 , Ratio :  3.0 , N-th :  3  ]
 Result :  38.7

Time Complexity

The time complexity of this code is constant, O(1), as the calculations performed inside the nth_gp_element function involve basic arithmetic operations and exponentiation. The execution time remains constant regardless of the input values.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment