I want to calculate the number of frames found in an MP3 file.
The MP3 file format allows you to closely approximate a bit rate per second by breaking down the audio data in blocks of $S$ or $S + 1$ bytes. My problem is to find how many blocks of size $S$ and how many blocks of size $S + 1$ I need to have to precisely match the total size $T$ of a file.
This comes out as the following equation:
$$\large{(S, N, M, T) \in \Bbb Z^{4}, S N + (S + 1) M = T}$$
I know $S$ and $T$, I need to find $N$ and $M$ knowing that all the numbers are elements of $\Bbb Z$ (i.e. integers). We also know that $T \gg S$.
Note: it is also assumed that $T$ is correct and indeed represents a exact multiple of $N$ and $M$ frames.
So how do I calculate N and M without iterating through all the possible numbers?
You can rewrite your equation as $$S(N+M) + M =T$$ Now compute $T$ divided by $S$ with a remainder. The remainder is $M$ and the integer result of the division is $N+M$, so you can solve for $N$. Note that this equation in general has lots of solutions but this will give you one solution.