-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_algorithm.js
More file actions
240 lines (210 loc) · 8.12 KB
/
basic_algorithm.js
File metadata and controls
240 lines (210 loc) · 8.12 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
/**
* Basic IVI Algorithm - Simplest implementation without pruning
*
* This is the minimal IVI implementation that only enforces the core
* IVI constraint without any global feasibility pruning.
*
* Requires common.js to be loaded first.
*
* @module basic_algorithm
*/
(function() {
'use strict';
/**
* Basic work function - only enforces IVI constraint, no pruning
*/
function workFunction(input) {
const { k, p_history, q_history, P_value, Q_value, carry_in, N_digits, N } = input;
if (!N_digits || k < 1 || k > N_digits.length) return [];
if (P_value === undefined || Q_value === undefined || N === undefined) {
throw new Error('P_value, Q_value, and N (BigInt) are required');
}
const target_digit = N_digits[k - 1];
const nextStates = [];
const isLastDigit = k === N_digits.length;
// Pre-compute base sum for terms i=2 to k-1
let baseSum = 0;
for (let i = 2; i < k; i++) {
const p_idx = i - 1;
const q_idx = k - i;
if (p_idx < p_history.length && q_idx >= 0 && q_idx < q_history.length) {
baseSum += multiplyDigits(p_history[p_idx], q_history[q_idx]);
}
}
const q1 = q_history.length > 0 ? q_history[0] : 0;
const p1 = p_history.length > 0 ? p_history[0] : 0;
// Explore all possible digit pairs (0-9 for each)
for (let pk = 0; pk <= 9; pk++) {
for (let qk = 0; qk <= 9; qk++) {
const sumOfProducts = k === 1
? multiplyDigits(pk, qk)
: baseSum + multiplyDigits(p1, qk) + multiplyDigits(pk, q1);
const total = sumOfProducts + carry_in;
// IVI Constraint: total = n_k + 10*c_{k+1}
if (total < target_digit) {
continue;
}
const remainder = total - target_digit;
if (remainder % 10 === 0) {
const carry_out = remainder / 10;
// At the last digit, final carry must be 0
if (carry_out >= 0 && carry_out <= 10 && (!isLastDigit || carry_out === 0)) {
const next_p_history = [...p_history, pk];
const next_q_history = [...q_history, qk];
// Update P_value and Q_value incrementally
const powerK = powerOf10(k - 1);
const new_P_value = P_value + BigInt(pk) * powerK;
const new_Q_value = Q_value + BigInt(qk) * powerK;
const lastTwoDigits = `${pk}${qk}`.padStart(2, '0');
const next_k = k + 1;
nextStates.push({
k: next_k,
p_history: next_p_history,
q_history: next_q_history,
P_value: new_P_value,
Q_value: new_Q_value,
carry_in: carry_out,
pk: pk,
qk: qk,
lastTwoDigits: lastTwoDigits
});
}
}
}
}
return nextStates;
}
function initializeAlgorithm(N) {
const N_big = typeof N === 'bigint' ? N : BigInt(N);
const N_digits = N_big.toString().split('').reverse().map(Number);
const N_display = N_big <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(N_big) : N_big.toString();
return {
p: null,
q: null,
N: N_display,
N_big: N_big,
N_digits: N_digits,
frontier: [{
k: 1,
p_history: [],
q_history: [],
P_value: 0n,
Q_value: 0n,
carry_in: 0,
N_digits: N_digits
}],
step: 0,
history: [],
activeBranches: 1,
maxActiveBranches: 1,
nodesVisited: 0,
nodesPruned: 0,
maxFrontierWidth: 1
};
}
function stepAlgorithm(state) {
if (state.maxSteps != null && state.step >= state.maxSteps) {
return { ...state, done: true };
}
const currentK = state.step + 1;
if (currentK > state.N_digits.length) {
return { ...state, done: true };
}
const target_digit = state.N_digits[currentK - 1];
const allResults = [];
let nodesVisited = state.nodesVisited || 0;
let nodesPruned = state.nodesPruned || 0;
state.frontier.forEach((branch, parentIdx) => {
const candidates = workFunction({
...branch,
k: currentK,
N_digits: state.N_digits,
N: state.N_big || BigInt(state.N)
});
nodesVisited += 100; // 10*10 digit pairs per branch
nodesPruned += (100 - candidates.length);
candidates.forEach(result => allResults.push({ ...result, parentIdx }));
});
if (state.maxFrontierSize != null && allResults.length > state.maxFrontierSize) {
allResults = allResults.slice(0, state.maxFrontierSize);
}
if (allResults.length === 0) {
return {
...state,
done: true,
activeBranches: 0,
maxActiveBranches: state.maxActiveBranches || 0,
nodesVisited: nodesVisited,
nodesPruned: nodesPruned,
maxFrontierWidth: state.maxFrontierWidth || 0
};
}
if (currentK === state.N_digits.length) {
for (let branchIdx = 0; branchIdx < allResults.length; branchIdx++) {
const branch = allResults[branchIdx];
if (branch.carry_in === 0 && checkSolution(branch, state.N)) {
const p = branch.P_value;
const q = branch.Q_value;
const activeBranches = allResults.length;
const maxActiveBranches = Math.max(state.maxActiveBranches || 0, activeBranches);
const maxFrontierWidth = Math.max(state.maxFrontierWidth || 0, activeBranches);
return {
...state,
step: currentK,
frontier: allResults,
history: [...state.history, {
k: currentK,
target_digit: target_digit,
branches: mapBranchesToHistory(allResults, branchIdx)
}],
success: true,
foundP: p.toString(),
foundQ: q.toString(),
solutionPath: buildSolutionPath(branch),
activeBranches: activeBranches,
maxActiveBranches: maxActiveBranches,
nodesVisited: nodesVisited,
nodesPruned: nodesPruned,
maxFrontierWidth: maxFrontierWidth
};
}
}
const activeBranches = allResults.length;
const maxActiveBranches = Math.max(state.maxActiveBranches || 0, activeBranches);
const maxFrontierWidth = Math.max(state.maxFrontierWidth || 0, activeBranches);
return {
...state,
done: true,
activeBranches: activeBranches,
maxActiveBranches: maxActiveBranches,
nodesVisited: nodesVisited,
nodesPruned: nodesPruned,
maxFrontierWidth: maxFrontierWidth
};
}
const stepHistory = {
k: currentK,
target_digit: target_digit,
branches: mapBranchesToHistory(allResults)
};
const activeBranches = allResults.length;
const maxActiveBranches = Math.max(state.maxActiveBranches || 0, activeBranches);
const maxFrontierWidth = Math.max(state.maxFrontierWidth || 0, activeBranches);
return {
...state,
step: currentK,
frontier: allResults,
history: [...state.history, stepHistory],
activeBranches: activeBranches,
maxActiveBranches: maxActiveBranches,
nodesVisited: nodesVisited,
nodesPruned: nodesPruned,
maxFrontierWidth: maxFrontierWidth
};
}
// Export functions to window for browser use
if (typeof window !== 'undefined') {
window.initializeAlgorithm = initializeAlgorithm;
window.stepAlgorithm = stepAlgorithm;
}
})();