Program for sum of geometric series
A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and common ratio is denoted by r. The series looks like this :- a, ar, ar2, ar3, ar4, . . .. The task is to find the sum of such a series. Examples :
Input : a = 1
r = 0.5
n = 10
Output : 1.99805
Input : a = 2
r = 2
n = 15
Output : 65534
A Simple solution to calculate the sum of geometric series.
C++
#include<bits/stdc++.h>
using namespace std;
float sumOfGP( float a, float r, int n)
{
float sum = 0;
for ( int i = 0; i < n; i++)
{
sum = sum + a;
a = a * r;
}
return sum;
}
int main()
{
int a = 1;
float r = 0.5;
int n = 10;
cout << sumOfGP(a, r, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
static float sumOfGP( float a, float r, int n)
{
float sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum = sum + a;
a = a * r;
}
return sum;
}
public static void main(String args[])
{
int a = 1 ;
float r = ( float )( 1 / 2.0 ) ;
int n = 10 ;
System.out.printf( "%.5f" ,(sumOfGP(a, r, n)));
}
}
|
Python
def sumOfGP(a, r, n) :
sum = 0
i = 0
while i < n :
sum = sum + a
a = a * r
i = i + 1
return sum
a = 1
r = ( float )( 1 / 2.0 )
n = 10
print ( "%.5f" % sumOfGP(a, r, n)),
|
C#
using System;
class GFG {
static float sumOfGP( float a,
float r,
int n)
{
float sum = 0;
for ( int i = 0; i < n; i++)
{
sum = sum + a;
a = a * r;
}
return sum;
}
static public void Main ()
{
int a = 1;
float r = ( float )(1/2.0) ;
int n = 10 ;
Console.WriteLine((sumOfGP(a, r, n)));
}
}
|
Javascript
function sumOfGP(a, r, n) {
let sum = 0;
for (let i = 0; i < n; i++) {
sum = sum + a;
a = a * r;
}
return sum;
}
let a = 1;
let r = 0.5;
let n = 10;
console.log(sumOfGP(a, r, n))
|
PHP
<?php
function sumOfGP( $a , $r , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$sum = $sum + $a ;
$a = $a * $r ;
}
return $sum ;
}
$a = 1;
$r = 0.5;
$n = 10;
echo (sumOfGP( $a , $r , $n ));
?>
|
Output :
1.99805
Time Complexity: O(n)
Auxiliary Space: O(1)
An Efficient solution to solve the sum of geometric series where first term is a and common ration is r is by the formula :- sum of series = a(1 – rn)/(1 – r). Where r = T2/T1 = T3/T2 = T4/T3 . . . and T1, T2, T3, T4 . . . ,Tn are the first, second, third, . . . ,nth terms respectively. For example – The series is 2, 4, 8, 16, 32, 64, . . . upto 15 elements. In the above series, find the sum of first 15 elements where first term a = 2 and common ration r = 4/2 = 2 or = 8/4 = 2 Then, sum = 2 * (1 – 215) / (1 – 2). sum = 65534
C++
#include<bits/stdc++.h>
using namespace std;
float sumOfGP( float a, float r, int n)
{
return (a * (1 - pow (r, n))) / (1 - r);
}
int main()
{
float a = 2;
float r = 2;
int n = 15;
cout << sumOfGP(a, r, n);
return 0;
}
|
Java
import java.math.*;
class GFG{
static float sumOfGP( float a, float r, int n)
{
return (a * ( 1 - ( int )(Math.pow(r, n)))) /
( 1 - r);
}
public static void main(String args[])
{
float a = 2 ;
float r = 2 ;
int n = 15 ;
System.out.println(( int )(sumOfGP(a, r, n)));
}
}
|
Python
def sumOfGP( a, r, n) :
return (a * ( 1 - pow (r, n))) / ( 1 - r)
a = 2
r = 2
n = 15
print sumOfGP(a, r, n)
|
C#
using System;
class GFG {
static float sumOfGP( float a, float r, int n)
{
return (a * (1 - ( int )(Math.Pow(r, n)))) /
(1 - r);
}
public static void Main()
{
float a = 2;
float r = 2;
int n = 15;
Console.Write(( int )(sumOfGP(a, r, n)));
}
}
|
Javascript
function sumOfGP(a, r, n) {
return (a * (1 - Math.pow(r, n))) / (1 - r);
}
function main() {
let a = 2;
let r = 2;
let n = 15;
console.log(sumOfGP(a, r, n));
}
main();
|
PHP
<?php
function sumOfGP( $a , $r , $n )
{
return ( $a * (1 - pow( $r , $n ))) /
(1 - $r );
}
$a = 2;
$r = 2;
$n = 15;
echo (sumOfGP( $a , $r , $n ));
?>
|
Output :
65534
Time Complexity: Depends on implementation of pow() function in C/C++. In general, we can compute integer powers in O(Log n) time.
Space Complexity: O(1) since using constant variables
Last Updated :
07 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...