s4w3d0ff - 7 months ago 18

Python Question

A basic Fibonacci sequence (has exponential growth):

`>> seq = fib(8)`

>> seq

[0, 1, 1, 2, 3, 5, 8, 13]

If I add them up I get

`33`

`>>sum(seq)`

33

>>seq[7]/seq[6]

1.625

How would I generate a list that has exponential growth at rate of the "golden ratio" like a fib sequence does?

`>> expSeq(8,33)`

[0, 1, 1, 2, 3, 5, 8, 13]

>>expSeq(5, 2.28)

[0.12, 0.24, 0.36, 0.60, 0.96]

>> sum([0.12, 0.24, 0.36, 0.60, 0.96])

2.28

I really just want to split a number into a list that has exponential growth and preferably as close to the "golden ratio" (1.618033988749... such as a Fibonacci sequence) as possible, and the sum of the sequence be close to what it started from.

Answer

A discrete sequence of exponentially growing numbers is called a geometric progression. The sum is called a geometric series. The formula here can easily be solved to produce the sequence you need:

```
>>> n = 5
>>> r = (1 + 5 ** 0.5) / 2
>>> r
1.618033988749895
>>> total = 2.28
>>> a = total * (1 - r) / (1 - r ** n)
>>> a
0.13965250359560707
>>> sequence = [a * r ** i for i in range(n)]
>>> sequence
[0.13965250359560707, 0.22596249743170915, 0.36561500102731626, 0.5915774984590254, 0.9571924994863418]
>>> sum(sequence)
2.28
>>> sequence[1] / sequence[0]
1.618033988749895
>>> sequence[2] / sequence[1]
1.618033988749895
>>> sequence[2] / sequence[1] == r
True
```

It's also worth noting that both this problem and the original problem of the Fibonacci could be solved using a binary search / bisection method.

Source (Stackoverflow)

Comments