elem-test/test.js
2024-11-26 19:30:14 -05:00

204 lines
No EOL
5.4 KiB
JavaScript

const elems = {
'[]': 'Nothing',
'["Nothing","Nothing"]': 'Null',
'["Null","Nothing"]': 'Void',
'["Null","Null"]': 'Set',
'["Set","Set"]': 'Count',
'["Count","Set"]': 'Number',
'["Null","Number"]': 'Negative',
'["Negative","Negative"]': 'Positive',
'["Negative","Positive"]': 'Inverse',
'["Inverse","Number"]': 'Fraction',
'["Positive","Positive"]': 'Addition',
'["Addition","Set"]': 'Union',
'["Union","Void"]': 'Expand',
'["Fraction","Expand"]': 'Part',
'["Number","Part"]': 'Digit',
'["Digit","Expand"]': 'Letter',
'["Letter","Set"]': 'Word',
'["Inverse","Word"]': 'Not',
'["Not","Null"]': 'True',
'["Not","True"]': 'False',
'["True","False"]': 'Boolean',
'["Set","Boolean"]': 'Binary',
'["Set","Word"]': 'Statement',
'["Statement","Addition"]': 'Operator',
'["Binary","Operator"]': 'Function',
'["Nothing","Number"]': 'Zero',
'["Function","Zero"]': 'Algebra',
'["Inverse","Nothing"]': 'Everything',
'["Number","Everything"]': 'Infinity',
'["Inverse","Infinity"]': 'Infinitesimal',
'["Addition","Addition"]': 'Multiplication',
'["Inverse","Multiplication"]': 'Division',
'["Inverse","Addition"]': 'Subtraction',
'["Void","Subtraction"]': 'Distance',
'["Division","Distance"]': 'Slope',
'["Limit","Slope"]': 'Derivative',
'["Inverse","Derivative"]': 'Integral',
'["Derivative","Integral"]': 'Calculus',
'["Algebra","Calculus"]': 'Mathematics',
'["Algebra","Infinitesimal"]': 'Limit',
'["Mathematics","Expand"]': 'Deabstraction',
'["Mathematics","Deabstraction"]': 'Science',
'["Science","Union"]': 'Chemistry',
'["Inverse","Set"]': 'Member',
'["Member","Everything"]': 'Thing',
'["Chemistry","Thing"]': 'Chemical',
'["Multiplication","Multiplication"]': 'Exponentation',
'["Multiplication","Exponentation"]': 'Recursion',
'["Recursion","Void"]': 'Pattern',
'["Chemical","Pattern"]': 'Crystal',
'["Derivative","Deabstraction"]': 'Change',
'["Science","Change"]': 'Energy',
'["Energy","Chemimcal"]': 'Heat',
'["Not","Heat"]': 'Cold',
'["Crystal","Cold"]': 'Rock',
'["Heat","Rock"]': 'Lava',
'["Heat","Lava"]': 'Plasma',
'["Heat","Plasma"]': 'Star',
'["Rock","Star"]': 'Planet',
'["Void","Rock"]': 'Air',
'["Air","Energy"]': 'Wind',
'["Rock","Wind"]': 'Stone',
'["Stone","Wind"]': 'Cobblestone',
'["Cobblestone","Wind"]': 'Sand',
'["Cobblestone","Sand"]': 'Gravel',
'["Star","Chemical"]': 'Hydrogen',
'["Air","Hydrogen"]': 'Water',
'["Water","Void"]': 'Ocean',
'["Energy","Ocean"]': 'Life',
'["Gravel","Life"]': 'Dirt',
}
const compCache = {};
const deCompCache = {};
function indexr(n, log_mode = false) {
let j;
try {
j = JSON.parse(Object.keys(elems)[Object.values(elems).findIndex(x => x == n)]);
} catch (err) {
return (log_mode) ? 1 : 0n;
}
let c;
c = comp(j, false, log_mode);
if (!log_mode && c > (10n ** (10n ** 4n))) {
return 'oh no';
}
return c;
}
function compSub(a,b,log_mode = false) {
let bb = (typeof a == 'bigint' && typeof b == 'bigint');
if (log_mode && !bb) {
if (a < 100 && b < 100) {
a = 10 ** a;
b = 10 ** b;
return Math.log10((b * b + b) / 2 + a + 1);
}
return a * 2 + b * 2;
} else if (!log_mode && bb) {
return (b * b + b) / 2n + a + 1n;
}
return 'oh no';
}
function comp(e, ignore = false, log_mode = false) {
if (!e || typeof e != 'object' || e.length == 0) return (log_mode) ? 0 : 0n;
let a, b;
if (ignore) {
a = e[0];
b = e[1];
} else {
let [j, k] = e;
a = compCache[`${j}.${log_mode}`] || indexr(j, log_mode);
compCache[`${j}.${log_mode}`] = a;
b = compCache[`${k}.${log_mode}`] || indexr(k, log_mode);
compCache[`${k}.${log_mode}`] = b;
}
if (b > a) {
return compSub(a,b,log_mode);
} else if (a != 'oh no' && b != 'oh no') {
return compSub(b,a,log_mode);
} else {
return 'oh no';
}
}
function decomp(e) {
// if (e == 0) return 0;
if (typeof e != 'bigint') {
e = BigInt(isNaN(Number(e)) ? 0 : e);
}
if (e == 0n) return elems['[]'];
let s = 0n;
if (e > 0n) s = (bsqrt(4n * e + 1n) - 1n) / 2n;
let h = e - (s * s + s) / 2n - 1n;
let h2 = compCache[h] || decomp(h);
compCache[h] = h2;
let s2 = compCache[s] || decomp(s);
compCache[s] = s2;
let out = JSON.stringify([h2, s2]);
if (elems[out]) return elems[out];
out = JSON.stringify([s2, h2]);
if (elems[out]) return elems[out];
return [h2, s2]
}
function bsqrt(value) {
if (value < 0n) {
throw 'square root of negative numbers is not supported'
}
if (value < 2n) {
return value;
}
function newtonIteration(n, x0) {
const x1 = ((n / x0) + x0) >> 1n;
if (x0 === x1 || x0 === (x1 - 1n)) {
return x0;
}
return newtonIteration(n, x1);
}
return newtonIteration(value, 1n);
}
for (let i in elems) {
let compd;
try {
compd = indexr(elems[i]).toString(10);
} catch (err) {
}
if (compd == 'oh no') {
compd = `10^(${indexr(elems[i],true)})`;
}
compd += '';
let l = compd.length;
//compd = compd.slice(0, Math.min(l, 100));
console.log(`${elems[i]}: ${compd} (${l} chars)\n`)
}