Geometric series: calculate quotient and number of elements from sum and first & last element

Question:

Creating evenly spaced numbers on a log scale (a geometric progression) can easily be done for a given base and number of elements if the starting and final values of the sequence are known, e.g., with numpy.logspace and numpy.geomspace. Now assume I want to define the geometric progression the other way around, i.e., based on the properties of the resulting geometric series. If I know the sum of the series as well as the first and last element of the progression, can I compute the quotient and number of elements?

For instance, assume the first and last elements of the progression are a0 and an and the sum of the series should be equal to sn. I know from trial and error that it works out for n=9 and r≈1.404, but how could these values be computed?

Asked By: Pontis

||

Answers:

You have enough information to solve it:

Sum of series = a + a*r + a*(r^2) ... + a*(r^(n-1))
= a*((r^n)-1)/(r-1)
= a*((last element * r) - 1)/(r-1)

Given the sum of series, a, and the last element, you can use the above equation to find the value of r.
Plugging in values for the given example:

50 = 1 * ((15*r)-1) / (r-1)
50r - 50 = 15r - 1
35r = 49
r = 1.4

Then, using sum of series = a*((r^n)-1)/(r-1):

50 = 1*((1.4^n)-1)(1.4-1)
21 = 1.4^n
n = log(21)/log(1.4) = 9.04

You can approximate n and recalculate r if n isn’t an integer.

Answered By: Abhinav Mathur

We have to reconstruct geometric progesssion, i.e. obtain a, q, m (here ^ means raise into power):

a, a * q, a * q^2, ..., a * q^(m - 1)

if we know first, last, total:

first = a                       # first item
last  = a * q^(m - 1)           # last item
total = a * (q^m - 1) / (q - 1) # sum

Solving these equation we can find

a = first
q = (total - first) / (total - last)
m = log(last / a) / log(q)

if you want to get number of items n, note that n == m + 1

Code:

import math

...

def Solve(first, last, total):
    a = first
    q = (total - first) / (total - last)
    n = math.log(last / a) / math.log(q) + 1
    
    return (a, q, n);

Fiddle

If you put your data (1, 15, 50) you’ll get the solution

a = 1 
q = 1.4 
n = 9.04836151801382 # not integer

since n is not an integer you, probably want to adjust; let last == 15 be exact, when total can vary. In this case q = (last / first) ^ (1 / (n - 1)) and total = first * (q ^ n - 1) / (q - 1)

a = 1
q = 1.402850552006674
n = 9

total = 49.752 # now n is integer, but total <> 50
Answered By: Dmitry Bychenko

You have to solve the following two equations for r and n:

a:= An / Ao = r^(n - 1)

and

s:= Sn / Ao = (r^n - 1) / (r - 1)

You can eliminate n by

s = (r a - 1) / (r - 1)

and solve for r. Then n follows by log(a) / log(r) + 1.


In your case, from s = 50 and a = 15, we obtain r = 7/5 = 1.4 and n = 9.048...

It makes sense to round n to 9, but then r^8 = 15 (r ~ 1.40285) and r = 1.4 are not quite compatible.

Answered By: Yves Daoust
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.