关于「Luhn 的加倍:模 10 为什么漏掉 09 与 90」,《校验码代数与韦尔霍夫算法》给出的定量结论是什么?
结论:第二章拆开 Luhn 的加倍:它漏掉 09 与 90,但漏检中跳位换位占 0.696。
数据口径:书中录入量、出错率与成本均为示意参数;全部计算由书中程序生成;本书是非个人化经营建议。 书中以标注框区分已发表的证据、可运行的 Python 计算(19 段程序)、原典与作者的假说。
对照数据表
下表数字由本章 Python 程序原样打印,可重新运行复现。 程序只用 Python 标准库与结论吻合的打印值: 0.696, 09, 90
| digit d | 0 1 2 3 4 5 6 7 8 9 |
| dbl(d) | 0 2 4 6 8 1 3 5 7 9 |
| h = d-dbl(d) | 0 9 8 7 6 4 3 2 1 0 |
| g = d+dbl(d) | 0 3 6 9 2 6 9 2 5 8 |
| adjacent swaps unseen | 09>90 (2 of 90 ordered) |
| twin errors unseen | 22>55, 33>66, 44>77 (6 of 90 ordered) |
| test payload 1234509 | 12345096 ; slip 1234590 -> 12345906 ; same: True |
| test payload 7000022 | 70000229 ; slip 7000055 -> 70000559 ; same: True |
| jump swap 12345674 > 12543674 passes Luhn: True | |
| expected Luhn misses among 12112 errors | 142.2 |
| adjacent swap | 27.5 share 0.193 |
| twin | 4.5 share 0.031 |
| phonetic | 7.4 share 0.052 |
| jump swap | 99.0 share 0.696 |
| jump twin | 3.9 share 0.027 |
查看完整验算程序
# Chapter 2. p21: why Luhn's doubling misses 09/90 and three twin pairs.
# Luhn (US patent 2,950,048): from the right, the check digit and every
# second digit count as they are; the others are doubled and, if the
# result exceeds 9, reduced by 9. The total must be 0 mod 10.
# An adjacent pair (a, b) is read as a + dbl(b) or dbl(a) + b; swapping
# it goes unseen when h(a) = h(b) with h(d) = d - dbl(d) mod 10.
# A twin aa > bb goes unseen when g(a) = g(b), g(d) = d + dbl(d) mod 10.
# The last block splits Luhn's expected misses among Verhoeff's 12,112
# errors (p11 counts) by class, on 10-digit codewords.
import math
def dbl(d):
return 2 * d if d < 5 else 2 * d - 9
def luhn_digit(payload):
# 校验位:右起第一位载荷数字加倍
s = 0
for i, ch in enumerate(reversed(payload)):
d = int(ch)
s += dbl(d) if i % 2 == 0 else d # step
return (10 - s % 10) % 10
def luhn_ok(code):
s = 0
for i, ch in enumerate(reversed(code)):
d = int(ch)
s += dbl(d) if i % 2 else d # step
return s % 10 == 0
R = range(10)
print("digit d " + " ".join("%d" % d for d in R))
print("dbl(d) " + " ".join("%d" % dbl(d) for d in R))
print("h = d-dbl(d) " + " ".join("%d" % ((d - dbl(d)) % 10) for d in R))
print("g = d+dbl(d) " + " ".join("%d" % ((d + dbl(d)) % 10) for d in R))
adj = ["%d%d>%d%d" % (a, b, b, a) for a in R for b in R
if a < b and (a - dbl(a)) % 10 == (b - dbl(b)) % 10]
twin = ["%d%d>%d%d" % (a, a, b, b) for a in R for b in R
if a < b and (a + dbl(a)) % 10 == (b + dbl(b)) % 10]
print("adjacent swaps unseen: %s (2 of 90 ordered)" % ", ".join(adj))
print("twin errors unseen: %s (6 of 90 ordered)" % ", ".join(twin))
# 测试号:明显的示意载荷,非任何真实卡号或账号
for p, q in (("1234509", "1234590"), ("7000022", "7000055")):
cp, cq = luhn_digit(p), luhn_digit(q)
print("test payload %s -> %s%d ; slip %s -> %s%d ; same: %s"
% (p, p, cp, q, q, cq, cp == cq))
code = "1234567" + str(luhn_digit("1234567"))
jump = code[:2] + code[4] + code[3] + code[2] + code[5:]
print("jump swap %s > %s passes Luhn: %s" % (code, jump, luhn_ok(jump)))
# 按类别拆分 Luhn 的漏检(十位码字,穷举位置)
PAT = {
"adjacent swap": (1237, [((a, b), (b, a)) for a in R for b in R
if a != b]),
"twin": (67, [((a, a), (b, b)) for a in R for b in R if a != b]),
"phonetic": (59, [((a, 0), (1, a)) for a in range(2, 10)]
+ [((1, a), (a, 0)) for a in range(2, 10)]),
"jump swap": (99, [((a, c, b), (b, c, a)) for a in R for c in R
for b in R if a != b]),
"jump twin": (35, [((a, c, a), (b, c, b)) for a in R for c in R
for b in R if a != b]),
}
def val(k0, digs):
n = len(digs)
t = [dbl(d) if (k0 + n - 1 - i) % 2 else d for i, d in enumerate(digs)]
return int(math.fsum(t)) % 10
lost = {}
for name, (cnt, pairs) in PAT.items():
n = len(pairs[0][0])
tot = miss = 0
for k0 in range(10 - n + 1):
for o, e in pairs:
tot += 1
miss += val(k0, o) == val(k0, e) # step
lost[name] = cnt * miss / tot
allmiss = math.fsum(lost.values())
print("expected Luhn misses among 12112 errors: %.1f" % allmiss)
for name, v in lost.items():
print(" %-14s %5.1f share %.3f" % (name, v, v / allmiss))唯一数据源与引用
范雅福(Fan, Y.). (2026). 《校验码代数与二面体群韦尔霍夫算法、订单号汇款码书号单字误抄与相邻换位检出率母典》第 2 章「Luhn 的加倍:模 10 为什么漏掉 09 与 90」(版本 1.0.0). 5D Library. DOI 待发布
DOI 待发布 · 专著页正式发布后上线(本站暂无专著页) · 5DL-B-297 · v1.0.0 · CC-BY-NC-ND-4.0
BibTeX
@book{fan2026297,
author = {Fan, Yafu},
title = {The Algebra of Check Digits and Verhoeff's Dihedral-Group Code: Detection Rates for Single-Digit Slips and Adjacent Transpositions in Order, Remittance and Book Numbers},
year = {2026},
publisher = {5D Library},
edition = {1.0.0},
note = {DOI pending}
}