
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program for Sum of Infinite Terms in GP
What is Geometric Progression (GP)?
Geometric Progression (GP) is a sequence of numbers where each term is generated by multiplying the previous term by a constant value. This constant value in GP is known as the common ratio. In this article, we are going to discuss how to calculate the sum of infinite terms in a GP using different approaches in C++.
Note: The important condition to remember is that a positive valid sum of infinite GP is only possible if the absolute of the common value (r) is less than 1, i.e., ?r? < 1.
Methods to Calculate the Sum of Infinite Terms in GP
The following are the two different approaches (methods) to calculate the sum of infinite terms in GP:
Iterative Approach (Brute Force Approximation)
We use an iterative approach to calculate the sum of infinite terms in GP. We initialize a variable to store the sum of the terms and take the number of iterations. We set the first term a, and add the current term to the sum. Update the current term by multiplying it with the common ratio. This process will continue until a number of iterations and return the sum of infinite terms of GP.
Steps
- Check if |r| >= 1. If true, return
NAN
since the series doesn't converge. - Initialize
sum
andcurrentTerm
with the first term a. - Use a loop to iteratively calculate the sum of terms, stopping after a predefined maximum number of iterations to simulate the infinite terms.
Implementation
#include <bits/stdc++.h> using namespace std; double sumOfInfiniteTermsGP(double a, double r) { if (fabs(r) >= 1) { return NAN; } double sum = 0; double currentTerm = a; // We will take the maximum number of iterations int maxIterations = 10000; // Iteratively calculate the sum of terms while (maxIterations > 0) { sum += currentTerm; currentTerm *= r; maxIterations--; } return sum; } int main() { double ans = sumOfInfiniteTermsGP(12, 0.2); if (!isnan(ans)) { cout << "The sum of infinite terms in the GP is: " << ans << endl; } else { cout << "Sum does not converge as |r| >= 1" << endl; } return 0; }
Output
The sum of infinite terms in the GP is: 15
Complexity Analysis
- Time Complexity: O(n), where n is the number of iterations.
- Space Complexity: O(1), as no extra space is used.
Direct Formula Approach
We can use the direct formula to find the sum of infinite terms of a geometric progression series. The formula for finding the sum of infinite terms in GP is:
Sum of Infinite terms of GP = a / (1 - r) if ?r? < 1
Steps
- Define a function that takes two parameters: first term and common ratio.
- Check if the absolute value of r is greater than or equal to 1. If true, return
NAN
because the series does not converge. - Use the formula
Sum = a / (1 - r)
to calculate the sum of infinite terms of GP. - Finally, return the sum.
Implementation
#include <bits/stdc++.h> using namespace std; double sumOfInfiniteTermsGP(double a, double r) { if (fabs(r) >= 1) { return NAN; } return a / (1 - r); } int main() { double ans = sumOfInfiniteTermsGP(12, 0.2); if (!isnan(ans)) { cout << "The sum of infinite terms in the GP is: " << ans << endl; } else { cout << "Sum does not converge as |r| >= 1" << endl; } return 0; }
Output
The sum of infinite terms in the GP is: 15
Complexity Analysis
- Time Complexity: O(1), as the calculation is done in constant time.
- Space Complexity: O(1), as no extra space is used.