# Number of terms in Geometric Series with given conditions

A geometric progression is a sequence of integers b1, b2, b3, …, where for each i > 1, the respective term satisfies the condition b_{i} = b_{i-1} * q, where q is called the common ratio of the progression.

Given geometric progression b defined by two integers b1 and q, and m “bad” integers a1, a2, .., am, and an integer l, write all progression terms one by one (including repetitive) while condition |b_{i}| <= l is satisfied (|x| means absolute value of x).

Calculate how many numbers will be there in our sequence, or print “inf” in case of infinitely many integers.

**Note:** If a term equals one of the “bad” integers, skip it and move forward to the next term.

Examples:

Input : b1 = 3, q = 2, l = 30, m = 4 6 14 25 48 Output : 3 The progression will be 3 12 24. 6 will also be there but because it is a bad integer we won't include it Input : b1 = 123, q = 1, l = 2143435 m = 4 123 11 -5453 141245 Output : 0 As value of q is 1, progression will always be 123 and would become infinity but because it is a bad integer we won't include it and hence our value will become 0 Input : b1 = 123, q = 1, l = 2143435 m = 4 5234 11 -5453 141245 Output : inf In this case, value will be infinity because series will always be 123 as q is 1 and 123 is not a bad integer.

**Approach:**

We can divide our solution in different cases:

**Case 1:** If starting value of series is greater than the given limit, output is 0.

**Case 2:** If starting value of series or q is 0, there are three more cases:

**Case 2.a:** If 0 is not given as a bad integer, answer will become inf.

**Case 2.b:** If b1 != 0 but q is 0 and b1 is also not a bad integer, answer will become 1.

**Case 2.c:** If 0 is given as a bad integer and b1 = 0, answer will become 0.

**Case 3:** If q = 1 we will check if b1 is given as a bad integer or not. If it is then answer will be 0 else answer will be inf.

**Case 4:** If q = -1, check if b1 and -b1 is present or not, if they are present our answer will be 0 else our answer will be inf.

**Case 5:** If none of the above cases hold, simply run a loop for b1 till l and calculate the number of elements.

Below is the implementation of above approach:

## C++

`// CPP program to find number of terms ` `// in Geometric Series ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A map to keep track of the bad integers ` `map<` `int` `, ` `bool` `> mapp; ` ` ` `// Function to calculate No. of elements ` `// in our series ` `void` `progression(` `int` `b1, ` `int` `q, ` `int` `l, ` ` ` `int` `m, ` `int` `bad[]) ` `{ ` ` ` `// Updating value of our map ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `mapp[bad[i]] = 1; ` ` ` ` ` `// if starting value is greate ` ` ` `// r than our given limit ` ` ` `if` `(` `abs` `(b1) > l) ` ` ` `cout << ` `"0"` `; ` ` ` ` ` `// if q or starting value is 0 ` ` ` `else` `if` `(q == 0 || b1 == 0) ` ` ` `{ ` ` ` `// if 0 is not a bad integer, ` ` ` `// answer becomes inf ` ` ` `if` `(mapp[0] != 1) ` ` ` `cout << ` `"inf"` `; ` ` ` ` ` `// if q is 0 and b1 is not and b1 ` ` ` `// is not a bad integer, answer becomes 1 ` ` ` `else` `if` `(mapp[0] == 1 && mapp[b1] != 1) ` ` ` `cout << ` `"1"` `; ` ` ` ` ` `else` `// else if 0 is bad integer and ` ` ` `// b1 is also a bad integer, ` ` ` `// answer becomes 0 ` ` ` `cout << ` `"0"` `; ` ` ` `} ` ` ` `else` `if` `(q == 1) ` `// if q is 1 ` ` ` `{ ` ` ` `// and b1 is not a bad integer, ` ` ` `// answer becomes inf ` ` ` `if` `(mapp[b1] != 1) ` ` ` `cout << ` `"inf"` `; ` ` ` ` ` `else` `// else answer is 0 ` ` ` `cout << ` `"0"` `; ` ` ` `} ` ` ` `else` `if` `(q == -1) ` `// if q is -1 ` ` ` `{ ` ` ` `// and either b1 or -b1 is not ` ` ` `// present answer becomes inf ` ` ` `if` `(mapp[b1] != 1 || mapp[-1 * b1] != 1) ` ` ` `cout << ` `"inf"` `; ` ` ` ` ` `else` `// else answer becomes 0 ` ` ` `cout << ` `"0"` `; ` ` ` `} ` ` ` `else` `// if none of the above case is true, ` ` ` `// simpy calculate the number of ` ` ` `// elements in our series ` ` ` `{ ` ` ` `int` `co = 0; ` ` ` `while` `(` `abs` `(b1) <= l) { ` ` ` `if` `(mapp[b1] != 1) ` ` ` `co++; ` ` ` `b1 *= 1LL * q; ` ` ` `} ` ` ` `cout << co; ` ` ` `} ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `// starting value of series, ` ` ` `// number to be multiplied, ` ` ` `// limit within which our series, ` ` ` `// No. of bad integers given ` ` ` `int` `b1 = 3, q = 2, l = 30, m = 4; ` ` ` ` ` `// Bad integers ` ` ` `int` `bad[4] = { 6, 14, 25, 48 }; ` ` ` ` ` `progression(b1, q, l, m, bad); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find number of terms ` `# in Geometric Series ` ` ` `# A map to keep track of the bad integers ` `mpp` `=` `dict` `() ` ` ` `# Function to calculate No. of elements ` `# in our series ` `def` `progression(b1, q, l, m, bad): ` ` ` ` ` `# Updating value of our map ` ` ` `for` `i ` `in` `range` `(m): ` ` ` `mpp[bad[i]] ` `=` `1` ` ` ` ` `# if starting value is greate ` ` ` `# r than our given limit ` ` ` `if` `(` `abs` `(b1) > l): ` ` ` `print` `(` `"0"` `,end` `=` `"") ` ` ` ` ` `# if q or starting value is 0 ` ` ` `elif` `(q ` `=` `=` `0` `or` `b1 ` `=` `=` `0` `) : ` ` ` ` ` `# if 0 is not a bad integer, ` ` ` `# answer becomes inf ` ` ` `if` `(` `0` `not` `in` `mpp.keys()): ` ` ` `print` `(` `"inf"` `,end` `=` `"") ` ` ` ` ` `# if q is 0 and b1 is not and b1 ` ` ` `# is not a bad integer, answer becomes 1 ` ` ` `elif` `(mpp[` `0` `] ` `=` `=` `1` `and` `b1 ` `not` `in` `mpp.keys()) : ` ` ` `print` `(` `"1"` `,end` `=` `"") ` ` ` ` ` `else` `: ` ` ` `# b1 is also a bad integer, ` ` ` `# answer becomes 0 ` ` ` `print` `(` `"0"` `,end` `=` `"") ` ` ` `elif` `(q ` `=` `=` `1` `): ` `# if q is 1 ` ` ` `# and b1 is not a bad integer, ` ` ` `# answer becomes inf ` ` ` `if` `(b1 ` `not` `in` `mpp.keys()) : ` ` ` `print` `(` `"inf"` `,end` `=` `"") ` ` ` `else` `: ` `# else answer is 0 ` ` ` `print` `(` `"0"` `,end` `=` `"") ` ` ` ` ` `elif` `(q ` `=` `=` `-` `1` `): ` `# if q is -1 ` ` ` `# and either b1 or -b1 is not ` ` ` `# present answer becomes inf ` ` ` `if` `(b1 ` `not` `in` `mpp.keys() ` `or` `-` `1` `*` `b1 ` `not` `in` `mpp.keys()) : ` ` ` `print` `(` `"inf"` `,end` `=` `"") ` ` ` ` ` `else` `:` `# else answer becomes 0 ` ` ` `print` `(` `"0"` `,end` `=` `"") ` ` ` `else` `:` `# if none of the above case is true, ` ` ` `# simpy calculate the number of ` ` ` `# elements in our series ` ` ` `co ` `=` `0` ` ` `while` `(` `abs` `(b1) <` `=` `l): ` ` ` `if` `(b1 ` `not` `in` `mpp.keys()): ` ` ` `co` `+` `=` `1` ` ` `b1 ` `*` `=` `q ` ` ` `print` `(co,end` `=` `"") ` ` ` ` ` `# Driver code ` ` ` `# starting value of series, ` `# number to be multiplied, ` `# limit within which our series, ` `# No. of bad integers given ` `b1 ` `=` `3` `q ` `=` `2` `l ` `=` `30` `m ` `=` `4` ` ` `# Bad integers ` `bad` `=` `[` `6` `, ` `14` `, ` `25` `, ` `48` `] ` ` ` `progression(b1, q, l, m, bad) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

3

This article is contributed by **Sarthak Kohli**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find n terms of Fibonacci type series with given first two terms
- Program for sum of geometric series
- Program for N-th term of Geometric Progression series
- Sum of the series 5+55+555+.. up to n terms
- Sum of first n terms of a given series 3, 6, 11, .....
- Sum of the first N terms of the series 2, 6, 12, 20, 30....
- Sum of the first N terms of the series 5,12, 23, 38....
- Sum of the first N terms of the series 2,10, 30, 68,....
- Find the sum of n terms of the series 1,8,27,64 ....
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.