brain

tamnd's digital brain — notes, problems, research

41319 notes

TAOCP 4.1 Exercise 19

Let $D$ be a set of $b$ integers such that for each residue $j$ modulo $b$, there exists exactly one element $a \in D$ with a \equiv j \pmod{b}, \quad 0 \le j < b.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 4.1 Exercise 20

**Exercise 4.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 4.1 Exercise 17

Let $\beta = i + 1$.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 4.1 Exercise 16

Let (a_n a_{n-1} \ldots a_1 a_0)_{i-1} = \sum_{j=0}^{n} a_j (i-1)^j be a number in the $(i-1)$-ary system, where each digit satisfies

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 4.1 Exercise 18

Let $B = i - 1$ denote the base of the number system under consideration.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 4.1 Exercise 15

We correct the missing justification by giving a fully constructive expansion algorithm for the negative decimal system and then completing the standard argument for the quater-imaginary system.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 4.1 Exercise 14

Let the base be $2i$, as in the quater-imaginary system.

taocpmathematicsalgorithmsvolume-1simple
CF 104664E - Riddle Me This (Easy Version)

We are given an even number of arrays, each array is a permutation of numbers from 1 to some common length $n$. The key operation allowed on any array is a cyclic rotation, where the last element moves to the front.

codeforcescompetitive-programming
TAOCP 4.1 Exercise 13

Let the negadecimal base be $\beta = -10$ and digits be $0,1,\dots,9$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 4.1 Exercise 11

Let $(a_n \ldots a_1 a_0)_{-2}$ and $(b_n \ldots b_1 b_0)_{-2}$ be given, where each digit $a_i, b_i \in {0,1}$.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 4.1 Exercise 12

A signed magnitude binary number $\pm(a_n \ldots a_1 a_0)_2$ represents an integer $N$ obtained by first forming the binary value A = \sum_{i=0}^{n} a_i 2^i and then setting $N = A$ if the sign is $+$...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 4.1 Exercise 10

Let a number be represented in _mixed-radix_ notation, that is, by a sequence of digits $(d_0, d_1, \ldots, d_{n-1})$ associated with radices $(b_0, b_1, \ldots, b_{n-1})$, where each $d_i$ satisfies...

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 4.1 Exercise 9

To convert an octal number to hexadecimal, we first expand each octal digit into a 3-bit binary block, concatenate all the blocks, and then regroup the resulting binary string into 4-bit blocks to obt...

taocpmathematicsalgorithmsvolume-1simple
TAOCP 4.1 Exercise 7

For integers represented in $n$ digits in base $10$, ten’s complement is defined modulo $10^n$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 4.1 Exercise 8

Let $F(x)$ be a distribution function as defined in Section 4.

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 4.1 Exercise 6

Let $p$ denote the total number of bits.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 4.1 Exercise 5

Let $x$ be a nonnegative integer written with $n$ decimal digits.

taocpmathematicsalgorithmsvolume-1
TAOCP 4.1 Exercise 4

We are asked to determine the position of the radix point in registers A and X after executing two sequences of MIX instructions, given that the operands in memory locations A and B have the radix poi...

taocpmathematicsalgorithmsvolume-1simple
TAOCP 4.1 Exercise 2

In signed magnitude representation, one digit records the sign and the remaining digits represent the magnitude in ordinary binary.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 4.1 Exercise 3

**Exercise 4.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 3.6 Exercise 14

Let X_n=(X_{n-37}+X_{n-100})\bmod 2 and define

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.6 Exercise 15

The correct approach is to treat `ran_array` as a black-box generator that produces a fresh block of $1009$ random integers each time it is called, while internally maintaining its own state.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 4.1 Exercise 1

We are asked to represent each integer in the range from $-10$ to $10$ using the numeral system with radix $-2$.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.6 Exercise 12

The machine performs integer arithmetic only in the range $[-32768,.,.,32767]$, which corresponds to a word size of $2^{16}$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.6 Exercise 13

The runtime error `ValueError: min() arg is empty` comes from trying to compute `min(arr)` when `arr` is empty.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.6 Exercise 9

The previous solution failed because it replaced the actual structure of **run_array** with an unmotivated generic LFSR model.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.6 Exercise 11

Let $S = 2^{-30} = 1/m$, where $m = 2^{30} = \text{MM}$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.6 Exercise 10

The failure in the previous solution is that it reconstructs a plausible subtractive generator, but it does not faithfully translate the _actual control structure of the C `run_start`_, in particular...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.6 Exercise 8

The original proposal correctly identified a sensible three-layer testing strategy, but it relied on an incorrect and oversimplified description of LCG period behavior.

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 3.6 Exercise 7

Let X_{n+1} \equiv aX_n + c \pmod{2^{35}}, \qquad 0 \le X_n < 2^{35}, and the observed data be

taocpmathematicsalgorithmsvolume-1math-project
TAOCP 3.6 Exercise 5

This exercise is intentionally open-ended.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 3.6 Exercise 6

A proper response to this exercise is necessarily organizational and empirical rather than purely mathematical, since it concerns existing implementations in a real subroutine library.

taocpmathematicsalgorithmsvolume-1project
TAOCP 3.6 Exercise 4

The exercise requires a concrete implementation that plays **two games**, uses a **specific random number generator**, shuffles a deck of cards, and prints all results in the indicated form.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.6 Exercise 2

Lady Lovelace's statement is correct in the limited sense that a machine executes rules supplied by its designer.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.6 Exercise 3

We simulate the game commonly known as _craps_.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.5 Exercise 44

The runtime error in the previous testing framework occurs because the `solve()` function is defined in the global scope, but the `run()` helper function tries to call it inside a new `io.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.5 Exercise 43

We are given a set of boxes, each containing some number of candies.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 3.6 Exercise 1

Method (1) of Section 3.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.5 Exercise 41

\textbf{Let }p_j=P(A,H_j)\qquad(0\le j\le N), where $H_j$ is the hybrid source whose first $j$ bits come from $S$ and whose remaining $N-j$ bits are independent unbiased bits.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.5 Exercise 42

Let $X_1, \ldots, X_n$ be random variables with \mu = \mathrm{E}X_j, \qquad \sigma^2 = \mathrm{E}X_j^2 - (\mathrm{E}X_j)^2 \quad (1 \le j \le n), and assume that for $i \ne j$,

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.5 Exercise 39

Let $(X_n)$ be a binary sequence that is random according to Definition R6.

taocpmathematicsalgorithmsvolume-1hm-project
TAOCP 3.5 Exercise 38

We restart from the actual content of the exercise: this is a counting problem in algorithmic (Kolmogorov) randomness, not a string manipulation task.

taocpmathematicsalgorithmsvolume-1math-research
TAOCP 3.5 Exercise 40

We are asked to simulate a dynamic seating scenario.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.5 Exercise 36

Let $(X_n)$ be a binary sequence that is random according to Definition R6.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.5 Exercise 37

The original argument fails because it treats sparsity as if it were automatically invisible to selection rules, and it treats adaptive selection as probabilistic.

taocpmathematicsalgorithmsvolume-1math-project
TAOCP 3.5 Exercise 33

Let ${U_n}$ be an infinite sequence, and let ${r_n}$ and ${s_n}$ be strictly increasing sequences of integers with no common elements, so that the subsequences ${U_{r_n}}$ and ${U_{s_n}}$ are disjoint...

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 35

We are asked to construct arrays called beautiful arrays.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.5 Exercise 34

Exercise 31 shows that Definition R5 does not imply Definition R1.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.5 Exercise 32

Let $\langle X_n \rangle$ be a $b$-ary sequence that is random according to Definition R5.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.5 Exercise 28

The failure of the previous solution comes from trying to _infer 3-distribution from digitwise uniformity of a single sequence_.

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 29

Let $X_0, X_1, X_2, \ldots$ be a $(2k)$-distributed binary sequence.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.5 Exercise 27

Let $B_n$ be the indicator of the event $V_n \ge \tfrac12$.

taocpmathematicsalgorithmsvolume-2
TAOCP 3.5 Exercise 26

The proposed solution does address the actual exercise and identifies the key idea: each pair $(U_{2n-1},U_{2n})$ is transformed by either keeping it unchanged or swapping coordinates, depending on wh...

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.5 Exercise 23

Let $(U_n)$ be a $[0,1)$ sequence.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.5 Exercise 24

Part (a) asks for a characterization of equidistribution in terms of the difference sequences $V_n^{(k)}=(U_{n+k}-U_n)\bmod 1,\qquad k>0.$ The statement to be proved is that $(U_n)$ is equidistributed...

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.5 Exercise 25

Let $(U_n)$ be a $\{0,1\}$-valued sequence that is equidistributed in $[0,1)$.

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 22

Let $Y_n = (U_n, U_{n+1}, \ldots, U_{n+k-1}) \in [0,1)^k.$ By Definition B in Section 3.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.5 Exercise 20

Let l_n^{(1)} \ge l_n^{(2)} \ge \cdots \ge l_n^{(n)} denote the lengths of the $n$ subintervals determined by the first $n$ points

taocpmathematicsalgorithmsvolume-1hm-project
TAOCP 3.5 Exercise 19

Let $U_0, U_1, \ldots$ be an infinite sequence of real numbers in $[0,1)$.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.5 Exercise 18

Let V_n=\frac{\lfloor nU_n\rfloor}{n}.

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 17

Let U_n = r^n \bmod 1, \qquad n \ge 0, where $r$ is a rational number.

taocpmathematicsalgorithmsvolume-1hm-research
TAOCP 3.5 Exercise 16

**Solution to Exercise 3.

taocpmathematicsalgorithmsvolume-1hm-project
TAOCP 3.5 Exercise 14

Let $\langle U_n \rangle$ be an $\infty$-distributed sequence.

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 15

Let Y_i=X_{f(n-1)+i}\qquad (i\ge 1).

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.5 Exercise 12

Let $(U_n)$ be a $k$-distributed sequence.

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 13

Let $A_n=\{\alpha \le U_n < \beta\},\qquad p=\beta-\alpha.$ Since $(U_n)$ is $\infty$-distributed, it is $k$-distributed for every positive integer $k$.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.5 Exercise 11

**Solution to Exercise 3.

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 3.5 Exercise 10

The proof of Theorem C uses the hypothesis \(m \mid q\) at the point where one must show that the residue classes \[ 0,\; m,\; 2m,\; \ldots,\; \left(\frac{q}{m}-1\right)m \] are distinct modulo \(q\).

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 9

Lemma E states that if \lim_{n\to\infty}\frac1n\sum_{j=1}^n y_{jn}=a, \qquad \lim_{n\to\infty}\frac1n\sum_{j=1}^n y_{jn}^2=a^2,

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 7

The answer is **No**.

taocpmathematicsalgorithmsvolume-1hm-hard
TAOCP 3.5 Exercise 8

I carefully analyzed why the previous code produced the wrong output on the first sample (`1010` with `k=0`).

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 3.5 Exercise 6

I analyzed the original solution carefully and identified the root cause of the wrong output.

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 5

We are given a binary string of length $n$ and we want to minimize a sum computed from all consecutive pairs of digits.

taocpmathematicsalgorithmsvolume-1hm-medium
TAOCP 3.5 Exercise 4

Let A(n) = S(n) \land T(n), \qquad B(n) = S(n) \lor T(n), and, for $N \ge 1$, let

taocpmathematicsalgorithmsvolume-1hm-simple
TAOCP 3.5 Exercise 3

Let the ternary sequence be periodic with period $P$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.5 Exercise 2

Let ${q_1, \ldots, q_N}$ be defined by $q_k = U_k / w_k$, where $U_1, \ldots, U_N$ are independent uniform $(0,1)$ random variables, and let $r$ be the $n$th smallest element of ${q_1, \ldots, q_N}$.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.4.2 Exercise 19

Let a file of $N$ items be given, with positive weights $w_1, \ldots, w_N$.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.5 Exercise 1

No.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.4.2 Exercise 16

The rejection method in the proposed solution does not satisfy the required expected $O(n)$ time bound when $n$ is comparable to $N$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.4.2 Exercise 18

**Corrected Solution for Exercise 3.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.4.2 Exercise 14

The key fact is the following characterization.

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.4.2 Exercise 15

We are asked to simulate **the effect of Algorithm P truncated at $j = t-n$**, using only $O(n)$ memory.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 3.4.2 Exercise 12

**Exercise 3.

taocpmathematicsalgorithmsvolume-1math-project
TAOCP 3.4.2 Exercise 11

Let $N$ be the total number of records in the input file.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.4.2 Exercise 13

The proposed solution answers the exercise that was actually asked.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.4.2 Exercise 10

Algorithm R uses the reservoir only because the final sample must ultimately be written in the same order as the corresponding records appeared in the input file.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.4.2 Exercise 9

We apply **Algorithm R** with reservoir size $n=3$ to a file containing 20 records numbered $1$ through $20$.

taocpmathematicsalgorithmsvolume-1simple
TAOCP 3.4.2 Exercise 7

Let $S$ be any fixed subset of $n$ records chosen from the $N$ available records.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.4.2 Exercise 8

Let $X$ denote the number of input records skipped before the first selection in Algorithm S.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.4.2 Exercise 6

Let $T$ be the value of $t$ when Algorithm S terminates.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.4.2 Exercise 5

Let $T$ denote the value of $t$ when Algorithm S terminates.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.4.2 Exercise 3

We are asked to determine if an array of integers is 3SUM-closed, which means that for every triple of distinct elements in the array, the sum of those three elements is itself present somewhere in th...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.4.2 Exercise 4

Let $p(m,t)$ denote the probability that exactly $m$ items are selected from among the first $t$ in Algorithm S, where $0 \le t \le N$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.4.2 Exercise 1

We are asked to **explain Eq.

taocpmathematicsalgorithmsvolume-1math-simple
TAOCP 3.4.2 Exercise 2

Algorithm S proceeds by examining the input file sequentially, maintaining two counters: $t$, the number of records remaining in the input file, and $n$, the number of additional records still to be s...

taocpmathematicsalgorithmsvolume-1medium
TAOCP 3.4.1 Exercise 31

Let $N(0,1)$ denote the normal distribution with mean $0$ and variance $1$.

taocpmathematicsalgorithmsvolume-1hm-project
TAOCP 3.4.1 Exercise 33

Algorithms P, M, F, and R generate a normal deviate by reading a sequence of independent uniform random variables U_1,U_2,U_3,\ldots, stopping after a random number of them has been examined.

taocpmathematicsalgorithmsvolume-1hard
TAOCP 3.4.1 Exercise 30

Let $\mu > 0$ be a given constant.

taocpmathematicsalgorithmsvolume-1math-hard
TAOCP 3.4.1 Exercise 29

We seek $n$ random variables $X_1, \ldots, X_n$ satisfying $0 \le X_1 \le \cdots \le X_n \le 1$, such that the joint distribution is uniform over all nondecreasing sequences in $[0,1]^n$.

taocpmathematicsalgorithmsvolume-1math-medium
TAOCP 3.4.1 Exercise 28

**Problem.

taocpmathematicsalgorithmsvolume-1hm-hard