-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
668 lines (601 loc) · 24.6 KB
/
index.html
File metadata and controls
668 lines (601 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>9178 论证器</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js" crossorigin="anonymous"></script>
<style>
:root {
--bg: #0f172a;
--panel: #111827;
--card: #1f2937;
--accent: #f97316;
--accent-2: #22d3ee;
--text: #e5e7eb;
--muted: #94a3b8;
--border: #1e293b;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
font-family: "Sora", "Manrope", "Segoe UI", sans-serif;
background: radial-gradient(120% 120% at 20% 20%, rgba(34, 211, 238, 0.18), transparent),
radial-gradient(140% 140% at 80% 0%, rgba(249, 115, 22, 0.12), transparent),
var(--bg);
color: var(--text);
display: flex;
align-items: center;
justify-content: center;
padding: 32px 16px;
}
.shell {
width: min(1080px, 100%);
background: var(--panel);
border: 1px solid var(--border);
border-radius: 18px;
box-shadow: 0 30px 80px rgba(0, 0, 0, 0.35);
padding: 24px;
display: grid;
grid-template-columns: 1.1fr 0.9fr;
gap: 18px;
}
@media (max-width: 900px) {
.shell {
grid-template-columns: 1fr;
}
}
h1 {
margin: 0 0 8px;
font-size: 26px;
letter-spacing: 0.3px;
}
.muted {
color: var(--muted);
font-size: 14px;
}
.card {
background: var(--card);
border: 1px solid var(--border);
border-radius: 14px;
padding: 16px;
}
label {
display: block;
margin-bottom: 6px;
font-weight: 600;
}
input[type="number"],
select {
width: 100%;
padding: 10px 12px;
border-radius: 10px;
border: 1px solid var(--border);
background: #0b1220;
color: var(--text);
font-size: 15px;
}
button {
border: none;
background: linear-gradient(120deg, #40ffd4, #ff80dd);
color: #0f172a;
font-weight: 700;
padding: 12px 16px;
border-radius: 12px;
cursor: pointer;
font-size: 15px;
transition: transform 0.1s ease, box-shadow 0.1s ease;
width: 100%;
}
button:active {
transform: translateY(1px);
}
.row {
display: flex;
gap: 12px;
align-items: center;
}
.row>* {
flex: 1;
}
.output {
font-family: "JetBrains Mono", "SFMono-Regular", Consolas, monospace;
background: #0b1220;
padding: 12px;
border-radius: 10px;
border: 1px solid var(--border);
min-height: 80px;
line-height: 1.4;
overflow-wrap: anywhere;
}
.pill {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 10px;
border-radius: 999px;
background: rgba(34, 211, 238, 0.12);
color: var(--text);
font-size: 13px;
}
.grid {
display: grid;
gap: 10px;
}
.status {
font-size: 14px;
color: var(--muted);
}
.latex-box {
margin-top: 12px;
padding: 12px;
border-radius: 10px;
background: #0b1220;
border: 1px solid var(--border);
min-height: 60px;
}
.latex-card {
display: flex;
flex-direction: column;
height: 100%;
}
.footer-link {
align-self: flex-end;
margin-top: auto;
font-size: 13px;
}
.warn {
color: #fbbf24;
}
.error {
color: #f87171;
}
</style>
</head>
<body>
<div class="shell">
<div class="grid" style="gap:14px;">
<div>
<h1>9178 论证器</h1>
<div class="muted">用 9,1,7,8 + - * / sqrt exp ln 与整数幂 [-1,2] 近似表示目标数</div>
</div>
<div class="card grid">
<div>
<label for="target">目标值</label>
<input id="target" type="number" step="0.001" value="3.14" />
</div>
<div class="row">
<div>
<label for="blocks">最多块数</label>
<input id="blocks" type="number" min="1" max="8" step="1" value="7" />
</div>
<div>
<label for="tol">容差</label>
<input id="tol" type="number" step="0.0001" value="0.0001" />
</div>
</div>
<button id="solve">开始求解</button>
<div class="status" id="status">等待输入…</div>
</div>
<div class="card grid">
<div class="row">
<div class="pill" id="delta">Δ = —</div>
<div class="pill" id="value">≈ —</div>
</div>
<div class="output" id="expr">结果会显示在这里</div>
</div>
</div>
<div class="card latex-card">
<div style="margin-top:12px; font-weight:700;">LaTeX</div>
<div class="latex-box" id="latex-box">等待表达式…</div>
<div class="muted footer-link">https://github.com/LemonQu-GIT/Express9178</div>
</div>
</div>
<script>
// Core numeric helpers
const LIMIT = 10n ** 12n;
const EPS = 1e-3;
const MAX_PER_LEVEL = 50000;
const baseOps = ['+', '-', '*', '/'];
const powExponents = [-1, 2];
const unaryFns = ['sqrt', 'exp', 'ln'];
const prec = (op) => {
if (op === '+' || op === '-') return 1;
if (op === '*' || op === '/') return 2;
if (op === '^') return 3;
return 4;
};
const gcd = (a, b) => {
a = a < 0n ? -a : a;
b = b < 0n ? -b : b;
while (b !== 0n) {
const t = a % b;
a = b; b = t;
}
return a === 0n ? 1n : a;
};
const makeFrac = (n, d) => {
if (d === 0n) return null;
if (n === 0n) return { n: 0n, d: 1n };
const g = gcd(n, d);
n /= g; d /= g;
if (d < 0n) { n = -n; d = -d; }
if (n > LIMIT || n < -LIMIT || d > LIMIT || d < -LIMIT) return null;
return { n, d };
};
const fracToNumber = (f) => Number(f.n) / Number(f.d);
const applyFracBinary = (op, a, b) => {
let n = 0n, d = 0n;
switch (op) {
case '+': n = a.n * b.d + b.n * a.d; d = a.d * b.d; break;
case '-': n = a.n * b.d - b.n * a.d; d = a.d * b.d; break;
case '*': n = a.n * b.n; d = a.d * b.d; break;
case '/': if (b.n === 0n) return null; n = a.n * b.d; d = a.d * b.n; break;
default: return null;
}
return makeFrac(n, d);
};
const powFrac = (a, k) => {
if (k === 0) return { n: 1n, d: 1n };
let numPow = 1n, denPow = 1n;
const absK = Math.abs(k);
for (let i = 0; i < absK; i++) {
numPow *= a.n; denPow *= a.d;
if (numPow > LIMIT || numPow < -LIMIT || denPow > LIMIT || denPow < -LIMIT) return null;
}
const res = k > 0 ? { n: numPow, d: denPow } : { n: denPow, d: numPow };
return makeFrac(res.n, res.d);
};
const keyFromFrac = (f) => `F:${f.n}/${f.d}`;
const keyFromFloat = (v) => {
if (!Number.isFinite(v)) return null;
const q = Math.round(v / EPS) * EPS;
return `N:${q}`;
};
const combineExprBinary = (left, op, right) => {
const p = prec(op);
const needL = !left.atomic && left.prec < p;
const needR = !right.atomic && (right.prec < p || (right.prec === p && (op === '-' || op === '/' || op === '^')));
const le = needL ? `(${left.expr})` : left.expr;
const re = needR ? `(${right.expr})` : right.expr;
return { expr: `${le}${op}${re}`, prec: p, atomic: false };
};
const combineExprUnary = (fn, child) => ({ expr: `${fn}(${child.expr})`, prec: 4, atomic: false });
const applyUnary = (fn, node) => {
const v = node.val;
if (!Number.isFinite(v)) return null;
if (fn === 'sqrt' && v < 0) return null;
if (fn === 'ln' && v <= 0) return null;
let res;
switch (fn) {
case 'sqrt': res = Math.sqrt(v); break;
case 'exp': res = Math.exp(v); break;
case 'ln': res = Math.log(v); break;
default: return null;
}
if (!Number.isFinite(res)) return null;
const exprPart = combineExprUnary(fn, node);
return { expr: exprPart.expr, prec: exprPart.prec, atomic: false, val: res, frac: null, fromUnary: true };
};
const applyPow = (node, k) => {
if (!Number.isFinite(node.val)) return null;
const v = Math.pow(node.val, k);
if (!Number.isFinite(v)) return null;
const baseNeedsPar = !node.atomic && node.prec < 3;
const baseStr = baseNeedsPar ? `(${node.expr})` : node.expr;
const expr = `${baseStr}^${k}`;
let frac = null;
if (node.frac) frac = powFrac(node.frac, k);
return { expr, prec: 3, atomic: false, val: v, frac, fromUnary: true };
};
const normalizeNode = (raw) => {
const frac = raw.frac ?? null;
const val = frac ? fracToNumber(frac) : raw.val;
return { expr: raw.expr, prec: raw.prec, atomic: raw.atomic, val, frac, fromUnary: Boolean(raw.fromUnary) };
};
const keyForNode = (node) => node.frac ? keyFromFrac(node.frac) : keyFromFloat(node.val);
// Load precomputed single-block data
async function loadBase() {
const resp = await fetch('cache.json');
if (!resp.ok) throw new Error('无法加载 cache.json');
const wrapped = await resp.json();
const data = Array.isArray(wrapped) ? wrapped : wrapped.data;
return data.map((x) => ({
expr: String(x.expr),
prec: Number(x.prec ?? 2),
atomic: Boolean(x.atomic),
frac: { n: BigInt(x.n), d: BigInt(x.d) },
val: Number(BigInt(x.n)) / Number(BigInt(x.d)),
fromUnary: false,
}));
}
function expandUnaries(node, seen, bucket) {
if (node.fromUnary) return;
for (const fn of unaryFns) {
const u = applyUnary(fn, node);
if (!u) continue;
const key = keyForNode(u);
if (!key || seen.has(key)) continue;
seen.add(key);
bucket.push(u);
}
for (const k of powExponents) {
const p = applyPow(node, k);
if (!p) continue;
const key = keyForNode(p);
if (!key || seen.has(key)) continue;
seen.add(key);
bucket.push(p);
}
}
function computeFloatBinary(op, a, b) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return b === 0 ? NaN : a / b;
default: return NaN;
}
}
function solve(target, base, maxBlocks = 7, tol = 1e-4) {
const levels = Array.from({ length: maxBlocks + 1 }, () => new Map());
const seen = new Set();
const targetNum = Number(target);
const frontier = [];
for (const r of base) {
const k = keyFromFrac(r.frac);
levels[1].set(k, { ...r, atomic: false, fromUnary: false });
seen.add(k);
frontier.push({ ...r, atomic: false, fromUnary: false });
if (Math.abs(r.val - targetNum) < tol) return r.expr;
}
let idx = 0;
while (idx < frontier.length) {
const node = frontier[idx++];
const bucket = [];
expandUnaries(node, seen, bucket);
for (const u of bucket) {
const ku = keyForNode(u);
if (!ku) continue;
if (!levels[1].has(ku)) levels[1].set(ku, u);
frontier.push(u);
if (Math.abs(u.val - targetNum) < tol) return u.expr;
}
}
for (let b = 2; b <= maxBlocks; b++) {
for (const [, prev] of levels[b - 1]) {
for (const blk of base) {
for (const op of baseOps) {
const fracVal = prev.frac && blk.frac ? applyFracBinary(op, prev.frac, blk.frac) : null;
const valNum = fracVal ? fracToNumber(fracVal) : computeFloatBinary(op, prev.val, blk.val);
if (!Number.isFinite(valNum)) continue;
const exprPart = combineExprBinary(prev, op, blk);
const node = normalizeNode({ expr: exprPart.expr, prec: exprPart.prec, atomic: false, val: valNum, frac: fracVal, fromUnary: false });
const k = keyForNode(node);
if (!k || seen.has(k)) continue;
seen.add(k);
levels[b].set(k, node);
if (Math.abs(node.val - targetNum) < tol) return node.expr;
const bucket = [];
expandUnaries(node, seen, bucket);
for (const u of bucket) {
const ku = keyForNode(u);
if (!ku) continue;
levels[b].set(ku, u);
if (Math.abs(u.val - targetNum) < tol) return u.expr;
}
}
}
}
if (levels[b].size > MAX_PER_LEVEL) {
const arr = Array.from(levels[b].values());
arr.sort((a, bnode) => Math.abs(a.val - targetNum) - Math.abs(bnode.val - targetNum));
const trimmed = arr.slice(0, MAX_PER_LEVEL);
const m = new Map();
for (const n of trimmed) {
const kk = keyForNode(n);
if (kk) m.set(kk, n);
}
levels[b] = m;
}
}
return null;
}
// UI wiring
const targetEl = document.getElementById('target');
const blocksEl = document.getElementById('blocks');
const tolEl = document.getElementById('tol');
const statusEl = document.getElementById('status');
const exprEl = document.getElementById('expr');
const deltaEl = document.getElementById('delta');
const valueEl = document.getElementById('value');
const btn = document.getElementById('solve');
const latexBox = document.getElementById('latex-box');
let baseData = null;
async function init() {
statusEl.textContent = '加载单块枚举中…';
try {
baseData = await loadBase();
statusEl.textContent = `已加载 ${baseData.length} 条单块结果。`;
} catch (e) {
statusEl.textContent = '加载失败:' + e.message;
statusEl.classList.add('error');
}
}
function formatDelta(actual, target) {
const d = actual - target;
return `Δ=${d.toExponential(3)}`;
}
// --- Minimal parser to convert expression string -> LaTeX ---
function tokenizeExpr(src) {
const tokens = [];
const re = /\s+|\d+(?:\.\d+)?|[a-zA-Z]+|[+\-*/^()]/y;
let m;
while ((m = re.exec(src)) !== null) {
const [lex] = m;
if (/^\s+$/.test(lex)) continue;
if (/^\d/.test(lex)) tokens.push({ type: 'num', value: lex });
else if (/^[a-zA-Z]+$/.test(lex)) tokens.push({ type: 'id', value: lex });
else tokens.push({ type: 'op', value: lex });
}
tokens.push({ type: 'eof', value: '' });
return tokens;
}
function parseToAst(src) {
const tokens = tokenizeExpr(src);
let i = 0;
const peek = () => tokens[i];
const eat = (v) => {
if (peek().value === v) { i++; return true; }
return false;
};
function parsePrimary() {
const t = peek();
if (t.type === 'num') { i++; return { kind: 'num', value: t.value, prec: 4 }; }
if (t.type === 'id') {
i++;
if (eat('(')) {
const arg = parseExpr();
if (!eat(')')) throw new Error('缺少 )');
return { kind: 'call', name: t.value, arg, prec: 4 };
}
return { kind: 'id', name: t.value, prec: 4 };
}
if (eat('(')) {
const e = parseExpr();
if (!eat(')')) throw new Error('缺少 )');
return { kind: 'group', child: e, prec: e.prec };
}
throw new Error('无法解析 token ' + t.value);
}
function parseUnary() {
if (eat('+')) return { kind: 'unary', op: '+', child: parseUnary(), prec: 3 };
if (eat('-')) return { kind: 'unary', op: '-', child: parseUnary(), prec: 3 };
return parsePrimary();
}
function parsePow() {
let node = parseUnary();
while (eat('^')) {
const right = parseUnary();
node = { kind: 'bin', op: '^', left: node, right, prec: 3 };
}
return node;
}
function parseTerm() {
let node = parsePow();
while (peek().value === '*' || peek().value === '/') {
const op = peek().value; i++;
const right = parsePow();
node = { kind: 'bin', op, left: node, right, prec: 2 };
}
return node;
}
function parseExpr() {
let node = parseTerm();
while (peek().value === '+' || peek().value === '-') {
const op = peek().value; i++;
const right = parseTerm();
node = { kind: 'bin', op, left: node, right, prec: 1 };
}
return node;
}
const ast = parseExpr();
if (peek().type !== 'eof') throw new Error('多余输入');
return ast;
}
function wrap(child, parentPrec) {
if (!child || child.prec >= parentPrec) return child.tex;
return `\\left(${child.tex}\\right)`;
}
function astToLatex(node) {
if (node.kind === 'num') return { tex: node.value, prec: node.prec };
if (node.kind === 'id') return { tex: node.name, prec: node.prec };
if (node.kind === 'group') return { tex: `\\left(${astToLatex(node.child).tex}\\right)`, prec: node.child.prec };
if (node.kind === 'unary') {
const inner = astToLatex(node.child);
return { tex: `${node.op}${wrap(inner, 3)}`, prec: node.prec };
}
if (node.kind === 'call') {
const arg = astToLatex(node.arg);
if (node.name === 'sqrt') return { tex: `\\sqrt{${arg.tex}}`, prec: 4 };
if (node.name === 'exp') return { tex: `e^{${arg.tex}}`, prec: 4 };
if (node.name === 'ln') return { tex: `\\ln\\left(${arg.tex}\\right)`, prec: 4 };
return { tex: `${node.name}\\left(${arg.tex}\\right)`, prec: 4 };
}
if (node.kind === 'bin') {
const l = astToLatex(node.left);
const r = astToLatex(node.right);
if (node.op === '+') return { tex: `${wrap(l, 1)}+${wrap(r, 1)}`, prec: 1 };
if (node.op === '-') return { tex: `${wrap(l, 1)}-${wrap(r, 1)}`, prec: 1 };
if (node.op === '*') return { tex: `${wrap(l, 2)}\\cdot${wrap(r, 2)}`, prec: 2 };
if (node.op === '/') return { tex: `\\frac{${l.tex}}{${r.tex}}`, prec: 2 };
if (node.op === '^') return { tex: `${wrap(l, 3)}^{${r.tex}}`, prec: 3 };
}
throw new Error('未知节点');
}
function renderLatex(target, expr) {
if (!latexBox) return;
if (!window.katex) {
latexBox.textContent = `${target} = ${expr}(KaTeX 未加载,使用纯文本)`;
return;
}
try {
const ast = parseToAst(expr);
const tex = astToLatex(ast).tex;
const full = `${target} = ${tex}`;
window.katex.render(full, latexBox, { throwOnError: false });
} catch (e) {
latexBox.textContent = 'LaTeX 渲染失败: ' + e.message;
}
}
btn.addEventListener('click', () => {
if (!baseData) { statusEl.textContent = '未加载到基础数据。'; return; }
const target = Number(targetEl.value);
const maxBlocks = Number(blocksEl.value);
const tol = Number(tolEl.value);
if (!Number.isFinite(target) || !Number.isFinite(maxBlocks) || !Number.isFinite(tol)) {
statusEl.textContent = '输入不合法。';
return;
}
statusEl.textContent = '搜索中…';
exprEl.textContent = '计算中…';
deltaEl.textContent = 'Δ = …';
valueEl.textContent = '≈ …';
latexBox.textContent = '渲染中…';
// Slight delay to allow UI refresh
setTimeout(() => {
const expr = solve(target, baseData, maxBlocks, tol);
if (expr) {
exprEl.textContent = expr;
// Evaluate expression numerically (unsafe but controlled input)
try {
const jsExpr = expr.replace(/\^/g, '**');
const val = Function(`with (Math) { const ln = Math.log; return (${jsExpr}); }`)();
deltaEl.textContent = formatDelta(val, target);
valueEl.textContent = `≈ ${val}`;
statusEl.textContent = '完成';
renderLatex(target, expr);
} catch (e) {
deltaEl.textContent = 'Δ = —';
valueEl.textContent = '≈ —';
statusEl.textContent = '完成(表达式回算失败)';
latexBox.textContent = 'LaTeX 渲染失败';
}
} else {
statusEl.textContent = '未在当前限制内找到表达式。';
exprEl.textContent = '未找到';
deltaEl.textContent = 'Δ = —';
valueEl.textContent = '≈ —';
latexBox.textContent = '未找到表达式';
}
}, 10);
});
init();
</script>
</body>
</html>