-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseName.js
More file actions
317 lines (312 loc) · 10.8 KB
/
BaseName.js
File metadata and controls
317 lines (312 loc) · 10.8 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
// Base Name Generator by Erik H
// declare all roots
// this will fill up with objects when getBestFactors is called
let factorArray = [];
factorArray["2"] = "root";
factorArray["3"] = "root";
factorArray["4"] = "root";
factorArray["5"] = "root";
factorArray["6"] = "root";
factorArray["7"] = "root";
factorArray["8"] = "root";
factorArray["9"] = "root";
factorArray["10"] = "root";
factorArray["11"] = "root";
factorArray["12"] = "root";
factorArray["13"] = "root";
factorArray["16"] = "root";
factorArray["17"] = "root";
factorArray["20"] = "root";
factorArray["36"] = "root";
factorArray["100"] = "root";
// predefine names so I don't need to create a specific cases for these
let numericNames = ["nullary", "unary"];
// ... and the same for abbreviations
let numericAbbreviation = ["nil", "uni"];
function getNumericName(num, suffix = true, first = true) {
let neg = false;
if (num < 0n) {
neg = true;
num *= -1n;
}
let key = num.toString();
let component = "";
if (first && suffix && numericNames[key]) {
component = numericNames[key];
} else {
let factors = getBestFactors(num);
if (factors == "root") {
switch (num) {
case 2n:
component = suffix ? "binary" : "bi";
break;
case 3n:
component = suffix ? "trinary" : "tri";
break;
case 4n:
component = suffix ? "quaternary" : "tetra";
break;
case 5n:
component = suffix ? "quinary" : "penta"
break;
case 6n:
component = suffix ? "seximal" : "hexa";
break;
case 7n:
component = suffix ? "septimal" : "hepta";
break;
case 8n:
component = suffix ? "octal" : "octo";
break;
case 9n:
component = suffix ? "nonary" : "enna";
break;
case 10n:
component = first ? "decimal" : suffix ? "gesimal" : "deca";
break;
case 11n:
component = suffix ? "elevenary" : "leva";
break;
case 12n:
component = suffix ? "dozenal" : "doza";
break;
case 13n:
component = first ? "baker's dozenal" : suffix ? "ker's dozenal" : "baker";
break;
case 16n:
component = suffix ? "hex" : "tesser";
break;
case 17n:
component = suffix ? "suboptimal" : "mal";
break;
case 20n:
component = suffix ? "vigesimal" : "icosi";
break;
case 36n:
component = suffix ? "niftimal" : "feta";
break;
case 100n:
component = suffix ? "centesimal" : "hecto";
break;
}
} else {
if (factors == "primeAboveRoot") {
if (suffix) {
component = "un" + getNumericName(num - 1n, true, false);
} else {
component = "hen" + getNumericName(num - 1n, false, false) + "sna";
}
} else if (factors.prime) {
if (suffix) {
component = "un" + getNumericName(factors.left, false, false) + getNumericName(factors.right, true, false);
} else {
component = "hen" + getNumericName(factors.left, false, false) + getNumericName(factors.right, false, false) + "sna";
}
} else {
component = getNumericName(factors.left, false, false) + getNumericName(factors.right, suffix, false);
}
}
if (first) {
let index = -1;
while ((index = component.search(/[ao][aeiou]/g)) != -1) {
component = component.substring(0, index) + component.substring(index + 1);
}
while ((index = component.search(/[i][iu]/g)) != -1) {
component = component.substring(0, index + 1) + component.substring(index + 2);
}
}
if (suffix && first && factors != "root") {
numericNames[key] = component;
}
}
if (neg) {
if (/^[aeiou]/.test(component)) {
component = "neg" + component;
} else {
component = "nega" + component;
}
}
return component;
}
function getNumericAbbreviation(num) {
let neg = false;
if (num < 0n) {
neg = true;
num *= -1n;
}
let key = num.toString();
let component = "";
if (numericAbbreviation[key]) {
component = numericAbbreviation[key];
} else {
let factors = getBestFactors(num);
if (factors == "root") {
switch (num) {
case 2n:
component = "b";
break;
case 3n:
component = "t";
break;
case 4n:
component = "T";
break;
case 5n:
component = "p";
break;
case 6n:
component = "h";
break;
case 7n:
component = "s";
break;
case 8n:
component = "o";
break;
case 9n:
component = "n";
break;
case 10n:
component = "d";
break;
case 11n:
component = "l";
break;
case 12n:
component = "D";
break;
case 13n:
component = "B";
break;
case 16n:
component = "x";
break;
case 17n:
component = "S";
break;
case 20n:
component = "i";
break;
case 36n:
component = "f";
break;
case 100n:
component = "c";
break;
}
} else {
if (factors == "primeAboveRoot") {
component = "[" + getNumericAbbreviation(num - 1n) + "]";
} else if (factors.prime) {
component = "[" + getNumericAbbreviation(factors.left) + getNumericAbbreviation(factors.right) + "]";
} else {
component = getNumericAbbreviation(factors.left) + getNumericAbbreviation(factors.right);
}
}
if (factors != "root") {
numericAbbreviation[key] = component;
}
}
if (neg) {
component = "-"+component;
}
return component;
}
// get the best factor pair that minimizes root count, the root count is < doBetterThan, and whose difference is minimal
function getBestFactors(num, doBetterThan = -1n) {
let key = num.toString();
let br = -1n;
let brc = (doBetterThan <= -2n) ? -1n : doBetterThan;
if (factorArray[key]) {
// skip idempotent work
if (factorArray[key]?.rootMinimum === undefined) {
// if best already found, return it
return factorArray[key];
} else if (brc != -1n && factorArray[key].rootMinimum >= doBetterThan) {
// if best not found, but less strictly than now, run out of factors immediately
return "runOut";
}
}
// if not root, can't do better than 1 root, so run out of factors immediately
if (brc == 1n || brc == 2n) return "runOut";
let isFirstFactor = true;
const factorsGenerator = factorsOf(num);
for (const factor of factorsGenerator) {
if (factor == 1n) {
if (isFirstFactor) {
// if it's prime
let returnVal = structuredClone(getBestFactors(num - 1n));
if (returnVal == "root") {
// special case for 36 and 100
returnVal = "primeAboveRoot";
} else {
returnVal.prime = true;
}
factorArray[key] = returnVal;
return returnVal;
}
if (br == -1n) {
// if no factor that exceeded expectations was found
// increase the minimum root requirement
factorArray[key] = { rootMinimum: doBetterThan };
return "runOut";
}
break;
}
isFirstFactor = false;
// get the best factors of factor
let lbf = getBestFactors(factor, brc - 1n);
// if things went poorly, continue to next factor
if (lbf == "runOut") continue;
// deal with roots not needing to be factored
if (lbf == "root" || lbf == "primeAboveRoot") {
lbf = { rootCount: 1n };
}
if (brc != -1n && lbf.rootCount >= brc) continue; //ignore if definitely worse
// repeat with other factor
let rbf = getBestFactors(num / factor, brc - lbf.rootCount);
if (rbf == "runOut") continue;
if (rbf == "root" || rbf == "primeAboveRoot") {
rbf = { rootCount: 1n };
}
if (lbf.rootCount + rbf.rootCount < brc || brc == -1n) {
// if best roots are found
br = factor;
brc = lbf.rootCount + rbf.rootCount;
if (brc == 2n) break; //escape early in best case
}
}
// assign results to list and return
let returnVal = { rootCount: brc, left: br, right: num / br, prime: false };
factorArray[key] = returnVal;
return returnVal;
}
// yields the factors of int from 1 to sqrt(int) in decending order
// ie. 400n -> 20n, 16n, 10n, 8n, 6n, 5n, 4n, 2n, 1n
function* factorsOf(int) {
// take log_10(sqrt(int)) "quickly"
let digits = 0n;
let pow = 1n;
while (pow < int) {
pow *= 10n;
digits++;
}
digits /= 2n;
// alternate add and subtracting decreasing powers of 10
let inc = true;
var i = 0n;
for (let j = digits; j >= 0n; j--) {
let p = 10n ** j;
if (inc) {
for (; i * i <= int; i += p) { }
} else {
for (; i * i > int; i -= p) { }
}
inc = !inc;
}
if (i * i > int) i--;
// go through each integer and yield factors
for (; i >= 1n; i--) {
if (int % i == 0n) yield i;
}
}