Open problems / 012

Folding a strip by x ↦ x/d and x ↦ x − k

A strip of $n$ pieces is folded evenly into $d$ parts, or has its last $k$ pieces folded in. Kagey asks for the least number of folds. His text and his figure stop the folding at different moments, so we count the folds between any two lengths instead. That count is a minimum of at most $1 + \log_d(n/y)$ base-$d$ digit sums, and every reading of the question reads off it.

The fold

A strip of toilet paper has $n$ pieces, and two folds are allowed. Fold it evenly into $d$ parts, which takes the length $x$ to $x/d$ and needs $d \mid x$. Fold the last $k$ pieces in, which takes $x$ to $x - k$ and needs $x > k$. A strip of length $0$ is not a strip.

Kagey asks for an efficient way to compute the least number of folds, $a_{d,k}(n)$. Two related questions come with it. What if you must keep folding until you cannot fold any longer? What is the least number of terminal pieces, and the least number of folds down to that number?

The text and the figure on the problem page stop the folding at different moments. The text stops once the strip is shorter than $k$ pieces. The figure takes $n = 13$, $d = 2$, $k = 3$ and shows $13 \to 10 \to 5 \to 2 \to 1$, four folds, passing through length $2$, which is already shorter than $3$. The only reference, A014701, also stops at $1$. So the page does not pin the definition down, and we do not choose. Every reading is a shortest path to a set of lengths in one graph, and we solve the problem for an arbitrary single target.

$D(n,y)$ is the least number of folds in a play from length $n$ to length $y$, and $\infty$ if there is none. A length $y$ is terminal when no fold is allowed, that is, $y \le k$ and $d \nmid y$. Write

$$\begin{aligned} A(n) &= D(n,1), & B(n) &= \min_{1 \le y < k} D(n,y) \quad (k \ge 2), \\ C(n) &= \min_{1 \le y \le k} D(n,y), & E(n) &= \min_{y \text{ terminal}} D(n,y), \\ E_m(n) &= D(n, m(n)), \end{aligned}$$

where $m(n)$ is the least terminal length that can be reached from $n$ at all.

$A$ is the reading of the figure and $B$ the reading of the text. $C$ counts folds until no fold of $k$ pieces is possible, $E$ answers the first related question, and $m$ and $E_m$ answer the second.

13 → 10 → 5 → 2 → 113−310÷25−32÷212 is below k = 3, where the text stops: 3 folds13 → 10 → 7 → 4 → 113−310−37−34−31both reach 1 in 4 folds
The two shortest plays of a strip of $13$ pieces with $d = 2$ and $k = 3$. In each strip the pieces that the next fold takes away are marked. The first play is the one drawn on Kagey’s problem page, alternating a fold of three with an even fold. The second folds three pieces in four times. Both take four folds, so $A(13) = 4$ and the bound in the figure is sharp. The first play passes through length $2$, which is already shorter than $k$, and that is where the text of the problem stops counting, after three folds.

The distance formula

A play with exactly $i$ even folds is described by the numbers $t_0, \dots, t_i \ge 0$ of $k$-folds before, between and after them. Undoing such a play from its end shows that it runs from $n$ to $y$ exactly when

$$n = d^i y + k \sum_{j=0}^{i} t_j d^j, \qquad t_j \in \mathbb{Z}_{\ge 0},$$

and that every intermediate length is long enough for the fold applied to it. The play uses $i + \sum_j t_j$ folds, so the question is how small $\sum_j t_j$ can be for a fixed weighted sum. If $t_j \ge d$ for some $j < i$, replace $(t_j, t_{j+1})$ by $(t_j - d, \, t_{j+1} + 1)$. The weighted sum does not move and the total drops by $d - 1$. That is a carry. In folds it says that $d$ folds of $k$ pieces followed by an even fold, $x \mapsto (x - dk)/d$, can be replaced by the even fold followed by one such fold, $x \mapsto x/d - k$. What survives the carries is a base-$d$ expansion.

Let $I(n,y)$ be the set of integers $i \ge 0$ with $d^i y \le n$ and $k \mid n - d^i y$, and put $M_i = (n - d^i y)/k$. Then

$$D(n,y) = \min_{i \in I(n,y)} \Bigl( i + \Bigl\lfloor \frac{M_i}{d^i} \Bigr\rfloor + s_d\bigl(M_i \bmod d^i\bigr) \Bigr),$$

where $s_d$ is the base-$d$ digit sum, and $D(n,y) = \infty$ exactly when $I(n,y)$ is empty. The $i$-th candidate is the least number of folds among plays with exactly $i$ even folds.

There are at most $1 + \log_d(n/y)$ candidates, and each costs one division by $k$ and one conversion to base $d$. So $D(n,y)$ is computed in time polynomial in $\log n$ for every $d$ and $k$, which answers Kagey’s Question for reading $A$. The same set says when a play exists at all: $y$ is reachable from $n$ exactly when $d^i y \equiv n \pmod{k}$ for some $i \ge 0$ with $d^i y \le n$.

Kagey’s example is $d = 2$, $k = 3$, $n = 13$, $y = 1$. Here $I = \{0, 2\}$ with $M_0 = 4$ and $M_2 = 3 = (11)_2$, so the candidates are $0 + 4 + 0 = 4$, four folds of three pieces, and $2 + 0 + 2 = 4$, the play of the figure. For $y = 2$ we get $I = \{1\}$, $M_1 = 3$ and $D(13,2) = 3$. Length $3$ is not reachable from $13$ at all, so $B(13) = C(13) = 3$.

For every $d \ge 2$ and $n \ge 1$,

$$a_{d,1}(n) = A(n) = C(n) = E(n) = \lfloor \log_d n \rfloor + s_d(n) - 1,$$

and the greedy play, which folds evenly whenever it can and otherwise folds one piece in, attains it.

For $d = 2$ that is A014701, the reference on the problem page. The formula is recorded in the entry and is classical, being the number of multiplications in left-to-right binary exponentiation. What the corollary adds is that the greedy process defining A014701 is a shortest play, so A014701 is $a_{2,1}$ under every reading of the question. We did not find the two-parameter problem treated anywhere.

When d and k are coprime

Let $\gcd(d,k) = 1$ and let $\kappa$ be the inverse of $k$ modulo $d$. Exactly one number of $k$-folds makes the next even fold possible, the residue $t \equiv \kappa x \pmod{d}$ in $\{0, \dots, d-1\}$, so the play is forced. Put $x_0 = n$ and $x_{i+1} = (x_i - t_i k)/d$ while that stays positive, and let $L$ be the last index. The chain is strictly decreasing, so $L \le \log_d n$, and $t_0, t_1, \dots$ are the first digits of the $d$-adic expansion of $n/k$.

Every shortest play is an initial segment of that chain followed by folds of $k$ pieces. Let $r_i \in \{1, \dots, k\}$ represent $x_i$ modulo $k$, let $c_i = i + \sum_{j < i} t_j$ be the folds spent getting to $x_i$, and put

$$f(i) = c_i + \Bigl\lfloor \frac{x_i - 1}{k} \Bigr\rfloor,$$

the cost of running down the chain to $x_i$ and then folding $k$ pieces in until $r_i$ pieces are left. The whole problem now lives on one chain of $O(\log n)$ numbers.

Let $i^*$ be the number of $j \in \{1, \dots, L\}$ with $d x_j > k$. Then $f$ is nonincreasing on $\{0, \dots, i^*\}$ and $f(i) = f(i^*) + (i - i^*)$ for $i^* \le i \le L$. Past $i^*$ the chain only divides, $r_{i+1} = r_i / d$, and $d \nmid r_L$.

Each reading is now the minimum of $f$ over the indices whose remainder qualifies, so one pass down the chain answers all of them: $C(n) = \min_i f(i)$ for the count that stops once no fold of $k$ pieces is left, $B(n) = \min\{f(i) : r_i < k\}$ for the reading of the text, $E(n) = \min\{f(i) : d \nmid r_i\}$ for the terminal one, and $A(n) = \min\{f(i) : r_i = 1\}$ for the figure.

$C(n) = f(i^*)$, and this is the number of folds made by the greedy rule: while the strip is longer than $k$ pieces, fold it evenly if you can, otherwise fold $k$ pieces in. If $k \ge 2$ then $B(n) = C(n)$ when $k \nmid n$, and $B(n) = \infty$ when $k \mid n$. Finally $E(n) = \min(f(i'), f(L))$, where $i'$ is the largest $i \le i^*$ with $d \nmid r_i$, and $E(n) = f(L)$ when there is no such $i'$.

So for coprime $d$ and $k$ the greedy rule is optimal for readings $B$ and $C$, for every $n$, and every reading costs $O(\log n)$ operations on numbers at most $n$.

01234567890510152025i, the number of even folds in the playfolds f(i)258533144256921287164A61132E61616C7888449221011greedyrxi* = 5
The forced chain of a strip of $533$ pieces with $d = 2$ and $k = 21$, with the chain values $x_i$ along the top and the remainders $r_i$ under the axis. The bars are the cost $f(i)$ of leaving the chain after $i$ even folds and then folding $21$ pieces in as far as they go. The cost is nonincreasing up to $i^* = 5$, where it is $6$, and rises by exactly one per step after it. The letters under the axis mark where each reading takes its value. $C$ takes the minimum. $E$ takes the least cost at an index whose remainder is not divisible by $d$, which is $6$ at $i = 4$, stopping at a strip of $11$ pieces. $A$ may only use the marked indices, so $A(533) = 7$ at $i = 3$, while the greedy rule for $A$ runs to the end of the chain and takes $10$.

Reading $A$ is where greedy breaks. The rule that defines A014701, fold evenly if you can, otherwise fold $k$ in if the strip is longer than $k$, otherwise stop, follows the whole chain and ends at $r_L$. It reaches a single piece exactly when $r_L = 1$, and it fails in two ways. For $(d,k) = (2,3)$ it plays $4 \to 2 \to 1$ when $4 \to 1$ is one fold. For $(2,5)$ it plays $6 \to 3$ and is stuck, although $6 \to 1$ is one fold.

Let $\gcd(d,k) = 1$, $k \ge 2$, and let $e = \operatorname{ord}_k(d)$. The greedy rule attains $A(n)$ for every $n$ with $A(n) < \infty$ if and only if

$$e = 1, \qquad \text{or} \qquad d^{\,e-1} < k \ \text{ and } \ \frac{d^{\,e}-1}{k} \ge e.$$
246810121416182022242628303223456789101112dk
Where the greedy rule is optimal for reaching a single piece, for $d$ from $2$ to $12$ down the rows and $k$ from $2$ to $32$ along the columns. A filled square is a pair where greedy is optimal for every $n$, a hollow square a pair where some $n$ defeats it, and a faint dot a pair whose $d$ and $k$ share a factor, which the theorem does not cover. The pattern is the criterion above, and it agrees with brute force for every $n \le 3000$. The ringed squares are the $14$ pairs in this range that a fitted guess predicted would fail.

That guess is worth recording. Fitted to $d \le 6$ and $k \le 7$, it said that greedy is optimal for reading $A$ exactly when $k \mid d - 1$, or $k = d + 1$ and $d \ge 3$. Its predictions for $d \le 12$ and $k \le 60$ were written down before that range was computed, and it is false. Greedy is also optimal for $(5,8)$, $(5,12)$, $(4,21)$ and $17$ further pairs. Every pair it called optimal is optimal, so only its “only if” half was wrong. The ledger in the repository holds the prediction and the refutation.

The least terminal length

The least reachable length is terminal, because a longer one can be folded again and so can one divisible by $d$. So the second related question is a question about congruences.

For $i \ge 0$ with $g_i = \gcd(d^i,k)$ dividing $n$, let $y_i$ be the least positive solution of $d^i y \equiv n \pmod{k}$. Then

$$m(n) = \min\{\, y_i : g_i \mid n, \ d^i y_i \le n \,\}, \qquad E_m(n) = D(n, m(n)).$$

If $\gcd(d,k) = 1$ then $m(n) = \min_{i \le L} r_i$, and for $n \ge k d^{\,e}$ with $e = \operatorname{ord}_k(d)$ it is the least element of the orbit of $n$ modulo $k$ under multiplication by $d^{-1}$, with representatives taken in $\{1, \dots, k\}$.

For $(d,k) = (2,3)$ the readings come out as follows. A dash means that a single piece cannot be reached. Here $B = C$ except at the multiples of $3$, where $B = \infty$, and $E_m = E$.

$n$12345678910111213141516
$A(n)$01–12–22–33–43–3
$C(n)$0001112222323333
$E(n)$0101212223324333
$m(n)$1131131131131131

When d and k share a factor

Let $g = \gcd(d,k) > 1$. The distance formula and the least terminal length still hold, so $A$, $m$ and $E_m$ still come from $O(\log n)$ candidates. What is lost is the forced chain. An optimal play still makes fewer than $d$ folds of $k$ pieces before each even fold, by the same carry, but the number $t < d$ with $d \mid x - tk$ is no longer unique. There are $g$ of them when $g \mid x$, and if $g \nmid x$ then no even fold is ever possible again. The choices lead to different lengths, and the greedy rule, which always takes the smallest $t$, is not optimal. For $d = k = 2$ it plays $18 \to 9 \to 7 \to 5 \to 3 \to 1$ in five folds, while $18 \to 16 \to 8 \to 4 \to 2$ takes four.

We compute $B$, $C$ and $E$ as minima of $D(n,y)$ over their targets $y \le k$, which costs at most $k(1 + \log_d n)$ candidates. That is polynomial in $\log n$ for fixed $k$, and not polynomial in $\log k$. The least $n$ at which greedy is not optimal for $C$, over the pairs with $\gcd(d,k) > 1$, $d \le 6$ and $k \le 7$, is $18$ at $(2,2)$, $68$ at $(2,4)$, $50$ at $(2,6)$, $30$ at $(3,3)$, $60$ at $(3,6)$, $36$ at $(4,2)$, $52$ at $(4,4)$, $108$ at $(4,6)$, $80$ at $(5,5)$, $78$ at $(6,2)$ and at $(6,3)$, $660$ at $(6,4)$, and $114$ at $(6,6)$. For coprime pairs there is no such $n$, by the theorem on readings $B$, $C$ and $E$.

What was checked

The script in the repository computes every quantity above by dynamic programming straight from the two folds, using no result of the paper, and compares.

  • The distance formula against the definition for all $1 \le y \le n \le 400$ and all $d \le 6$, $k \le 7$, and for $y = 1$ and all $n \le 10^5$.
  • For the coprime pairs and all $n \le 10^5$: $C(n) = f(i^*)$, the optimality of greedy for $C$, the statements on $B$ and $E$, the chain form of $A$, and, at every tenth $n$, the shape of $f$.
  • For the other pairs, $B$, $C$ and $E$ against the minima over their targets for all $n < 3000$ and every seventh $n \le 10^5$, with the first failures of greedy reproduced.
  • The criterion for greedy on reading $A$ against brute force, over all $378$ coprime pairs with $d \le 12$ and $2 \le k \le 60$ and all $n \le 3000$. Greedy is optimal throughout that range for exactly the $48$ pairs the criterion names.
  • $a_{2,1}$ against the $86$ terms of A014701 printed in the entry, and the $k = 1$ formula for $d \le 7$ and $n \le 10^5$.
  • $105$ random values of $n$ with $200$ digits, every target $y \le k$, against an independent memoised recursion. For instance $n = 10^{199} + 12$ has $A_{2,1}(n) = 883$, $A_{2,3}(n) = C_{2,3}(n) = 877$, $A_{3,7}(n) = 843$ and $C_{6,4}(n) = 861$.

The ledger holds the predictions written down before each run, with their outcomes, including the one that was refuted.

What is open

Two questions are left, and the first of them is the gap in the word “efficient”. When $\gcd(d,k) > 1$, can $C(n)$ be computed in time polynomial in $\log k$ as well as $\log n$? By the proof of the distance formula this asks for the minimum of $i + \lfloor M/d^i \rfloor + s_d(M \bmod d^i)$ over the solutions $M \ge 0$ of $kM \equiv n \pmod{d^i}$ among the $d^i$ consecutive integers ending at $\lceil n/k \rceil - 1$.

The second question is the first failure of greedy. Take the pairs the criterion rules out because $e \ge 2$ and $d^{\,e-1} < k$ and $(d^{\,e}-1)/k < e$. There are twelve of them with $d \le 12$ and $k \le 60$, and in every one the least $n$ at which greedy is not optimal for reading $A$ is $n = d^{\,e}$. That is computed, not proved.

For $k \ge 2$ we also do not see a closed form for $C(n)$ simpler than $f(i^*)$. The index $i^*$ depends on the size of $n$, and the digits on $n$ modulo the powers of $d$.