Home > OS >  Geometric series: calculate quotient and number of elements from sum and first and last element
Geometric series: calculate quotient and number of elements from sum and first and last element

Time:12-13

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. This works out for r and n as I know from trial and error, but how could these values be computed?

CodePudding user response:

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.

CodePudding user response:

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

  • Related