Skip to content

Commit daeeacc

Browse files
committed
improvements in the last post
1 parent 207da8f commit daeeacc

32 files changed

Lines changed: 295 additions & 275 deletions

File tree

404.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ <h2>Latest</h2>
570570

571571
<li><a href="/post/">Posts</a></li>
572572

573-
<li><a href="/post/perfect-distribution/">Perfect Distribution Based on GCD</a></li>
573+
<li><a href="/post/perfect-distribution/">Perfect Distribution: GCD in Disguise</a></li>
574574

575575
<li><a href="/post/binomial-modulo-prime/">Binomial Coefficients Modulo a Prime: Fermat&#39;s Theorem and the Non-Adjacent Selection Problem</a></li>
576576

content/post/perfect-distribution.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ date = "2017-08-01T07:29:43Z"
33
math = true
44
highlight = true
55
tags = ["math","python", "recursion", "recursive functions", "gcd", "uniform distribution"]
6-
title = "Perfect Distribution Based on GCD"
6+
title = "Perfect Distribution: GCD in Disguise"
77
draft = false
88

99
# Optional featured image (relative to `static/img/` folder).
@@ -13,9 +13,11 @@ caption = ""
1313

1414
+++
1515

16-
## Perfect Distribution Based on GCD
16+
## Perfect Distribution: GCD in Disguise
1717

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.
1921

2022
### Use Case 0: Text spacing
2123

@@ -48,6 +50,10 @@ Suppose we specify $230$ requests per second: then during every part, $2.3$ requ
4850
In other words, the tool must send 2 requests in most parts and 3 in some parts.
4951
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).
5052

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+
5157
We present the general solution ${\cal PD}$ to the problem—for any specified input in the above examples, ${\cal PD}$ produces the exact solution.
5258
Any probabilistic or approximation solutions do not achieve the required exactness and are usually more complicated.
5359
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
6268
* POST: $res = {\cal GCD}(a, n)$.
6369

6470
```python
65-
def gcd(a, n):
71+
def gcd(a: int, n: int) -> int:
6672
assert 0 <= a <= n
6773

6874
res = n
@@ -82,7 +88,7 @@ The recurrence mirrors Euclid's algorithm: ${\cal PD}(a, n)$ calls ${\cal PD}(n
8288
* POST: $res$ is perfectly distributed (gaps between consecutive ones differ by at most 1).
8389

8490
```python
85-
def pd(a, n):
91+
def pd(a: int, n: int) -> list[int]:
8692
assert 0 <= a <= n
8793

8894
res = [0] * n
@@ -97,6 +103,10 @@ def pd(a, n):
97103
return res
98104
```
99105

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+
100110
### Example. ${\cal PD}(2, 10)$: $a = 2$, $n = 10$, so $n - a = 8$ zeros
101111
We want to mix $2$ ones and $8$ zeros.
102112
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)] =
111121

112122
* ${\cal PD}(1, 10) : [1, 0, \ldots, 0] = 1, 0^9$
113123
* ${\cal PD}(2, 10) : [1, 0, 0, 0, 0, 1, 0, 0, 0, 0] = 1, 0^4 , 1, 0^4$
114-
* ${\cal PD}(3, 10) : [1, 0, 0, 1, 0, 0, 1, 0, 0, 0] = 1, 0^2 , 1, 0^2 , 1, 0^3$
124+
* ${\cal PD}(3, 10) : [1, 0, 0, 1, 0, 0, 1, 0, 0, 0] = 1, 0^2 , 1, 0^2 , 1, 0^3$ — visual: `1 _ _ 1 _ _ 1 _ _ _` (gaps between 1s: 2, 2, 3)
115125
* ${\cal PD}(4, 10) : [1, 0, 0, 1, 0, 1, 0, 0, 1, 0] = 1, 0^2 , 1, 0, 1, 0^2 , 1, 0$ $= (1, 0^2 , 1, 0)^2$
116126
* ${\cal PD}(5, 10) : [1, 0, 1, 0, 1, 0, 1, 0, 1, 0] = (1, 0)^5$
117127
* ${\cal PD}(6, 10) : [1, 0, 1, 1, 0, 1, 0, 1, 1, 0] = 1, 0, 1^2 , 0, 1, 0, 1^2 , 0$ $ = (1, 0, 1^2 , 0)^2$
@@ -128,7 +138,7 @@ The same recurrence can be expressed using subtraction instead of modulo, which
128138
**Slow GCD (subtraction-based):**
129139

130140
```python
131-
def gcd(a, n):
141+
def gcd(a: int, n: int) -> int:
132142
assert 0 <= a <= n
133143

134144
res = n
@@ -145,7 +155,7 @@ def gcd(a, n):
145155
**Slow PD (subtraction-based):**
146156

147157
```python
148-
def pd(a, n):
158+
def pd(a: int, n: int) -> list[int]:
149159
assert 0 <= a <= n
150160

151161
res = [0] * n

index.html

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@
341341
<meta property="og:description" content="Software Engineer" /><meta property="og:image" content="/media/icon_hu_43cf117bf1a42c34.png" /><meta property="og:locale" content="en-us" />
342342

343343

344-
<meta property="og:updated_time" content="2017-08-02T07:29:43&#43;00:00" />
344+
<meta property="og:updated_time" content="2026-02-08T19:47:43&#43;00:00" />
345345

346346

347347

@@ -975,18 +975,14 @@ <h1 class="mb-0">Recent Posts</h1>
975975
<div class="media-body">
976976

977977
<div class="section-subheading article-title mb-0 mt-0">
978-
<a href="/post/perfect-distribution/" >Perfect Distribution Based on GCD</a>
978+
<a href="/post/perfect-distribution/" >Perfect Distribution: GCD in Disguise</a>
979979
</div>
980980

981981

982982
<a href="/post/perfect-distribution/" class="summary-link">
983983
<div class="article-style">
984-
<h2 id="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-
<h3 id="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+
<h2 id="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>
990986
</div>
991987
</a>
992988

@@ -1019,7 +1015,7 @@ <h3 id="use-case-0-text-spacing">Use Case 0: Text spacing</h3>
10191015

10201016
<span class="middot-divider"></span>
10211017
<span class="article-reading-time">
1022-
6 min read
1018+
7 min read
10231019
</span>
10241020

10251021

0 commit comments

Comments
 (0)