Generate a chain of numbers with a specific total sum and a selectable function to generate those numbers

13 Views Asked by At

TL;DR

I would like to write a function that I could use in a spreadsheet to calculate a set of numbers from A to B with the following constraints:

  • The first number is given: A (e.g. 120)
  • The last number is given: B (e.g. 20)
  • The total is given: S (e.g. 3360)

The sum of all numbers should be less or equal S. For each number from A to B, each number is more than 0, and each next number is smaller than the previous number.

The difficult part is that all the generated numbers should approximate a curve when drawn on a chart, and so the function to generate those numbers should be one of those used to generate curves (bezier curve, parabole, free fall in Earth's gravity).

The simplest example with a linear function would be F(n) = X(n-1) - 3 Or in spreadsheet: A2=120, A3=A2-3, A4=A3-3, ...

Starting with A=120 this gives me pairs: (120, 0), (117, 1), (114, 2), (111, 3), ... Their sum is 2397 and last number B is 21. On a chart this draws a line from (120,0) to (21,33).

I am not an expert but I think in mathematical terms this could be written as:

Given A (first number), B (last number), and S (sum of all numbers) generate numbers X(1..n) using a few example functions (linear, bezier curve, parabole, free fall in Earth's gravity) so that:

  • 1st number X(1) is equal to A (e.g. 120)
  • last number X(n) is equal to B (e.g. 20)
  • sum of all numbers sum(X1...Xn) is <= S (e.g. <= 3360)
  • A < B
  • All numbers X(1)..X(n) are > 0
  • Each next number is smaller than previous one: X(n+1) < X(n)

Long story

I am editing a video with specific length: 56 seconds (or less). I want it to consist of a set of clips, each clip having a decreasing amount of frames. The question is about the mathematical function able to generate numbers of frames for each clip.

Since the budget is 56 seconds, the total amount of frames is 3360 (56*60). Therefore, the sum of all frames in all clips should be 3360 (or slightly less than that). The length of the first clip is 120 and last is 20 (or slightly more than that).

Since we know the total, first and last number, I think this should be enough information to generate a chain of numbers using a specific function.

If the function was linear, and disregarding the sum and the length of last clip, I could generate numbers like 120, 118, 116, ..., or 120, 117, 114, ...

The sum of numbers decreasing by 3, X(n+1) = X(n) + 3, from 120 to 21, gives me 2397 (generated manually).

Things get more complicated if I wanted to use Bezier curve, parabole, or another similar function to generate those numbers. How to approach such problem?