You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Optional featured image (relative to `static/img/` folder).
@@ -13,9 +13,11 @@ caption = ""
13
13
14
14
+++
15
15
16
-
## Perfect Distribution Based on GCD
16
+
## Perfect Distribution: GCD in Disguise
17
17
18
-
We discuss an algorithm that distributes $a$ ones among $n$ positions so that the gaps between consecutive ones differ by at most one—a **perfect distribution**. The examples below are software engineering use cases for this algorithm.
18
+
We discuss an algorithm that distributes $a$ ones among $n$ positions so that the gaps between consecutive ones differ by at most one—a **perfect distribution**. I developed it while working on profiling, stress, and negative testing of a system that needed exactly this kind of uniform spread. I am not aware of prior art; if you know of related work, I would be interested to hear.
19
+
20
+
The examples below are software engineering use cases for this algorithm.
19
21
20
22
### Use Case 0: Text spacing
21
23
@@ -48,6 +50,10 @@ Suppose we specify $230$ requests per second: then during every part, $2.3$ requ
48
50
In other words, the tool must send 2 requests in most parts and 3 in some parts.
49
51
We need to choose which $30$ parts get the extra request; ${\cal PD}(30, 100)$ yields that array (a $1$ in cell $i$ means part $i$ gets 3 requests).
50
52
53
+
### Why naive approaches fail
54
+
55
+
A natural idea is to place ones at multiples of $n/a$, e.g. `[1 if (i * a) % n < a else 0 for i in range(n)]`, or to use `round(i * a / n)` to decide. Such formulas can produce wrong counts (off-by-one), clusters at the boundaries, or gaps that differ by more than one. Another approach—repeatedly choosing the “emptiest” gap—avoids clustering but is harder to implement correctly and lacks the clean recurrence we want. ${\cal PD}$ gives the exact, unique fairest distribution in a few lines.
56
+
51
57
We present the general solution ${\cal PD}$ to the problem—for any specified input in the above examples, ${\cal PD}$ produces the exact solution.
52
58
Any probabilistic or approximation solutions do not achieve the required exactness and are usually more complicated.
53
59
The algorithm ${\cal PD}$ is simple, has a clear correctness proof, and admits $O(n)$ time and space complexity.
@@ -62,7 +68,7 @@ Moreover, it has a direct relation to Euclid's algorithm for computing the Great
* POST: $res$ is perfectly distributed (gaps between consecutive ones differ by at most 1).
83
89
84
90
```python
85
-
defpd(a, n):
91
+
defpd(a: int, n: int) -> list[int]:
86
92
assert0<= a <= n
87
93
88
94
res = [0] * n
@@ -97,6 +103,10 @@ def pd(a, n):
97
103
return res
98
104
```
99
105
106
+
**Why it works.** The algorithm treats $n$ positions as $a$ “blocks” of size $\lfloor n/a \rfloor$ or $\lceil n/a \rceil$. The recursive call ${\cal PD}(n \bmod a, a)$ decides which blocks get the extra slot (the remainder). Each block is filled by either repeating a perfect distribution of ones in a sub-interval (when $a \le n/2$) or by inverting zeros when $a > n/2$. The recurrence ensures that at every level, the pattern stays uniform—no block gets more than one extra slot, so gaps never differ by more than one.
107
+
108
+
**Optimality.** The perfect distribution (gaps differ by at most 1) is unique up to the choice of which gaps are $\lfloor (n-a)/a \rfloor$ and which are $\lceil (n-a)/a \rceil$. The algorithm constructs this by induction: the base case $a=0$ is trivial; for $a>0$, the recursive call yields a perfect distribution for the “remainder” subproblem, and the interleaving step preserves the gap invariant.
109
+
100
110
### Example. ${\cal PD}(2, 10)$: $a = 2$, $n = 10$, so $n - a = 8$ zeros
101
111
We want to mix $2$ ones and $8$ zeros.
102
112
The recursive call ${\cal PD}(0, 2)$ returns $[0, 0] = 0^2$, which is assigned to $arr$.
@@ -111,7 +121,7 @@ Then the array $res$ is filled as follows: $[(1, 0, 1, 1, 1), (1, 0, 1, 1, 1)] =
<h2id="perfect-distribution-based-on-gcd">Perfect Distribution Based on GCD</h2>
985
-
<p>We discuss an algorithm that distributes $a$ ones among $n$ positions so that the gaps between consecutive ones differ by at most one—a <strong>perfect distribution</strong>. The examples below are software engineering use cases for this algorithm.</p>
986
-
<h3id="use-case-0-text-spacing">Use Case 0: Text spacing</h3>
987
-
<p>Given a string of characters $s$ and a number $n$ greater than the length of $s$,
988
-
extend the string $s$ to length $n$ by inserting spaces between the words.
989
-
The key requirement is that the distances between two consecutive words be uniform.</p>
984
+
<h2id="perfect-distribution-gcd-in-disguise">Perfect Distribution: GCD in Disguise</h2>
985
+
<p>We discuss an algorithm that distributes $a$ ones among $n$ positions so that the gaps between consecutive ones differ by at most one—a <strong>perfect distribution</strong>. I developed it while working on profiling, stress, and negative testing of a system that needed exactly this kind of uniform spread. I am not aware of prior art; if you know of related work, I would be interested to hear.</p>
990
986
</div>
991
987
</a>
992
988
@@ -1019,7 +1015,7 @@ <h3 id="use-case-0-text-spacing">Use Case 0: Text spacing</h3>
0 commit comments