Posted on by Kalkicode
Code Mathematics

Find the sum of geometric series

A geometric series 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. The sum of a geometric series is a fundamental concept in mathematics with applications in various fields, including physics, finance, and engineering. This article aims to explain how to find the sum of a geometric series using a C program.

Problem Statement

Given the first term 'a', common ratio 'r', and the number of terms 'n', the problem is to find the sum of the first 'n' terms of the geometric series.

Example

For instance, let's consider a geometric series with a first term of 6, a common ratio of 2, and 6 terms. The sum of the first 6 terms of this series is 378. Similarly, for a series with a first term of 4, a common ratio of 3, and 7 terms, the sum is 4372. For a series with a first term of 4.3, a common ratio of 3, and 3 terms, the sum is 55.9.

Idea to Solve

The formula to calculate the sum of the first 'n' terms of a geometric series is given by:

Sum = a * (1 - r^n) / (1 - r)

Where:

  • 'a' is the first term of the series
  • 'r' is the common ratio
  • 'n' is the number of terms

Pseudocode

function sum_gp_element(a, ratio, n):
    Print "[ Start :", a, ", Ratio :", ratio, ", N :", n, " ]"
    Calculate the sum using the formula
    Print "Sum :", sum

function main():
    sum_gp_element(6, 2, 6)
        sum_gp_element(4, 3, 7)
        sum_gp_element(4.3, 3, 3)

Algorithm Explanation

  1. The sum_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 sum using the formula sum = a * (1 - pow(ratio, n)) / (1 - ratio).
  4. It prints the calculated sum.
  5. The main function tests the sum_gp_element function with different values.

Code Solution

// C program 
// Find the sum of geometric series
#include <stdio.h>

#include <math.h>

// Sum of geometric progression series
// a : starting point
// ratio : common ratio
// n : number of element
void sum_gp_element(double a, double ratio, int n)
{
	printf("\n [ Start : %lf, Ratio : %lf, N : %d ] ", a, ratio, n);
	//Calcuate sum 
	double sum = (a * (1 - (pow(ratio, n)))) / (1 - ratio);
	printf("\n Sum : %lf\n", sum);
}
// Driver code 
int main()
{
	// Test Cases
	sum_gp_element(6, 2, 6);
	sum_gp_element(4, 3, 7);
	sum_gp_element(4.3, 3, 3);
	return 0;
}
//compile the code like this
// compile : gcc -o a test.c -1m 
// here test.c is program file
// run : ./a.out

Output

 [ Start : 6.000000, Ratio : 2.000000, N : 6 ]
 Sum : 378.000000

 [ Start : 4.000000, Ratio : 3.000000, N : 7 ]
 Sum : 4372.000000

 [ Start : 4.300000, Ratio : 3.000000, N : 3 ]
 Sum : 55.900000
// Java program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	public void sum_gp_element(double a, double ratio, int n)
	{
		System.out.print("\n [ Start : " + a + ", Ratio : " + ratio + ", N : " + n + " ] ");
		//Calcuate sum 
		double sum = (a * (1 - (Math.pow(ratio, n)))) / (1 - ratio);
		System.out.print("\n Sum : " + sum + "\n");
	}
	public static void main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.sum_gp_element(6, 2, 6);
		obj.sum_gp_element(4, 3, 7);
		obj.sum_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 6.0, Ratio : 2.0, N : 6 ]
 Sum : 378.0

 [ Start : 4.0, Ratio : 3.0, N : 7 ]
 Sum : 4372.0

 [ Start : 4.3, Ratio : 3.0, N : 3 ]
 Sum : 55.9
//Include header file
#include <iostream>

#include<math.h>

using namespace std;
// C++ program
// Find the sum of geometric series
class MyMath
{
	public:
		// Sum of geometric progression series
		// Here
		// a : starting point
		// ratio : common ratio
		// n : number of element
		void sum_gp_element(double a, double ratio, int n)
		{
			cout << "\n [ Start : " << a << ", Ratio : " << ratio << ", N : " << n << " ] ";
			//Calcuate sum 
			double sum = (a * (1 - (pow(ratio, n)))) / (1 - ratio);
			cout << "\n Sum : " << sum << "\n";
		}
};
int main()
{
	MyMath obj = MyMath();
	// Test Cases
	obj.sum_gp_element(6, 2, 6);
	obj.sum_gp_element(4, 3, 7);
	obj.sum_gp_element(4.3, 3, 3);
	return 0;
}

Output

 [ Start : 6, Ratio : 2, N : 6 ]
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ]
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ]
 Sum : 55.9
//Include namespace system
using System;
// C# program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	public void sum_gp_element(double a, double ratio, int n)
	{
		Console.Write("\n [ Start : " + a + ", Ratio : " + ratio + ", N : " + n + " ] ");
		//Calcuate sum 
		double sum = (a * (1 - (Math.Pow(ratio, n)))) / (1 - ratio);
		Console.Write("\n Sum : " + sum + "\n");
	}
	public static void Main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.sum_gp_element(6, 2, 6);
		obj.sum_gp_element(4, 3, 7);
		obj.sum_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 6, Ratio : 2, N : 6 ]
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ]
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ]
 Sum : 55.9
<?php
// Php program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	public	function sum_gp_element($a, $ratio, $n)
	{
		echo "\n [ Start : ". $a .", Ratio : ". $ratio .", N : ". $n ." ] ";
		//Calcuate sum 
		$sum = ($a * (1 - (pow($ratio, $n)))) / (1 - $ratio);
		echo "\n Sum : ". $sum ."\n";
	}
}

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

Output

 [ Start : 6, Ratio : 2, N : 6 ]
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ]
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ]
 Sum : 55.9
// Node Js program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	sum_gp_element(a, ratio, n)
	{
		process.stdout.write("\n [ Start : " + a + ", Ratio : " + ratio + ", N : " + n + " ] ");
		//Calcuate sum 
		var sum = (a * (1 - (Math.pow(ratio, n)))) / (1 - ratio);
		process.stdout.write("\n Sum : " + sum + "\n");
	}
}

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

Output

 [ Start : 6, Ratio : 2, N : 6 ]
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ]
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ]
 Sum : 55.9
#  Python 3 program
#  Find the sum of geometric series

class MyMath :
	#  Sum of geometric progression series
	#  Here
	#  a : starting point
	#  ratio : common ratio
	#  n : number of element
	def sum_gp_element(self, a, ratio, n) :
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N : ", n ," ] ", end = "")
		# Calcuate sum 
		sum = (a * (1 - ((ratio**n)))) / (1 - ratio)
		print("\n Sum : ", sum ,"\n", end = "")
	

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

if __name__ == "__main__": main()

Output

 [ Start :  6 , Ratio :  2 , N :  6  ]
 Sum :  378.0

 [ Start :  4 , Ratio :  3 , N :  7  ]
 Sum :  4372.0

 [ Start :  4.3 , Ratio :  3 , N :  3  ]
 Sum :  55.9
#  Ruby program
#  Find the sum of geometric series
class MyMath

	#  Sum of geometric progression series
	#  Here
	#  a : starting point
	#  ratio : common ratio
	#  n : number of element
	def sum_gp_element(a, ratio, n)
	
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N : ", n ," ] ")
		# Calcuate sum 
		sum = (a * (1 - (ratio**n))) / (1 - ratio)
		print("\n Sum : ", sum ,"\n")
	end
end
def main()

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

Output

 [ Start : 6, Ratio : 2, N : 6 ] 
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ] 
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ] 
 Sum : 55.9
// Scala program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	def sum_gp_element(a: Double, ratio: Double, n: Int): Unit = {
		print("\n [ Start : " + a + ", Ratio : " + ratio + ", N : " + n + " ] ");
		//Calcuate sum 
		var sum: Double = a * (1 - (Math.pow(ratio, n))) / (1 - ratio);
		print("\n Sum : " + sum + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMath = new MyMath();
		// Test Cases
		obj.sum_gp_element(6, 2, 6);
		obj.sum_gp_element(4, 3, 7);
		obj.sum_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 6.0, Ratio : 2.0, N : 6 ]
 Sum : 378.0

 [ Start : 4.0, Ratio : 3.0, N : 7 ]
 Sum : 4372.0

 [ Start : 4.3, Ratio : 3.0, N : 3 ]
 Sum : 55.9
import Foundation
// Swift program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	func sum_gp_element(_ a: Double, _ ratio: Double, _ n: Int)
	{
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N : ", n ," ] ", terminator: "");
		//Calcuate sum 
		let sum: Double = (a * (1 - (pow(ratio, Double(n))))) / (1 - ratio);
		print("\n Sum : ", sum ,"\n", terminator: "");
	}
}
func main()
{
	let obj: MyMath = MyMath();
	// Test Cases
	obj.sum_gp_element(6, 2, 6);
	obj.sum_gp_element(4, 3, 7);
	obj.sum_gp_element(4.3, 3, 3);
}
main();

Output

 [ Start :  6.0 , Ratio :  2.0 , N :  6  ]
 Sum :  378.0

 [ Start :  4.0 , Ratio :  3.0 , N :  7  ]
 Sum :  4372.0

 [ Start :  4.3 , Ratio :  3.0 , N :  3  ]
 Sum :  55.9

Time Complexity

The time complexity of this code is constant, O(1), as the calculations performed inside the sum_gp_element function involve only 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