s4w3d0ff - 1 year ago 46

Python Question

A basic fib sequence:

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

If I add them up I get

`32`

How would I reverse this process and have a function that will take

`32`

`32`

`>> fibSeq(32)`

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

What about smaller numbers like

`2.28`

`[0.12, 0.24, 0.36, 0.60, 0.96]`

`2.28`

Is this even possible?

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.

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.