I have some computer code that outputs character sequences of various lengths. For readability, I would like to format output sequences as groups of $3$ or $4$ characters. Examples:
- The sequence
ABCDEFcould be formatted asABC DEF($2$ groups of $3$ characters) - The sequence
ABCDEFGHIJKcould be formatted asABC DEFG HIJK($1$ group of $3$ and $2$ groups of $4$ characters
It appears to me that any sequence with $6$ or more characters could be divided into groups of $3$ or $4$ characters without any remainder. That is, for a sequence of length $n$, where $n > 5$, it appears possible to always find a non-negative integer solution for $a$ and $b$ in the equation $ n = a \times 3 + b \times 4$.
To this, I have two math questions:
- Am I correct in my assumption that this is always possible?
- Is there a fast and direct way to compute $a$ and $b$ for a given $n$?
(1) Yes, and the bound of $6$ is sharp: Given coprime numbers $m, n$, the largest integer that cannot be written as a nonnegative integral combination of $m$ and $n$ (the so-called Frobenius number) is $m n - m - n$. This is the two-coin case of the Frobenius Coin Problem.
(2) Here's part of a "greedy algorithm" that minimizes the number of groups (i.e., maximizes the number of groupings of $4$): For a given sequence length $L$, let $q := \left\lfloor\frac{L}{4}\right\rfloor$ and let $r$ be the remainder of dividing $L$ by $4$, so that $L = 4 q + r$, $r \in \{0, 1, 2, 3\}$:
Can you work out the cases $r = 0, 1, 3$?
Note that in general there is more than one solution: For $L \geq 18$ we will have at least four groups of $3$ or three groups of $4$, but one can always replace one of these groupings with the other.