elem-test/test.js

204 lines
5.4 KiB
JavaScript
Raw Normal View History

2024-11-26 19:25:45 -05:00
const elems = {
2024-11-26 17:07:42 -05:00
'[]': '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',
2024-11-26 19:25:45 -05:00
'["Positive","Positive"]': 'Addition',
'["Addition","Set"]': 'Union',
'["Union","Void"]': 'Expand',
2024-11-26 17:07:42 -05:00
'["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',
2024-11-26 19:25:45 -05:00
'["Set","Word"]': 'Statement',
'["Statement","Addition"]': 'Operator',
2024-11-26 17:07:42 -05:00
'["Binary","Operator"]': 'Function',
2024-11-26 19:30:14 -05:00
'["Nothing","Number"]': 'Zero',
2024-11-26 17:07:42 -05:00
'["Function","Zero"]': 'Algebra',
2024-11-26 19:25:45 -05:00
'["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',
2024-11-26 17:07:42 -05:00
'["Algebra","Calculus"]': 'Mathematics',
2024-11-26 19:25:45 -05:00
'["Algebra","Infinitesimal"]': 'Limit',
'["Mathematics","Expand"]': 'Deabstraction',
2024-11-26 17:07:42 -05:00
'["Mathematics","Deabstraction"]': 'Science',
2024-11-26 19:25:45 -05:00
'["Science","Union"]': 'Chemistry',
'["Inverse","Set"]': 'Member',
'["Member","Everything"]': 'Thing',
'["Chemistry","Thing"]': 'Chemical',
'["Multiplication","Multiplication"]': 'Exponentation',
'["Multiplication","Exponentation"]': 'Recursion',
'["Recursion","Void"]': 'Pattern',
2024-11-26 17:07:42 -05:00
'["Chemical","Pattern"]': 'Crystal',
2024-11-26 19:25:45 -05:00
'["Derivative","Deabstraction"]': 'Change',
'["Science","Change"]': 'Energy',
'["Energy","Chemimcal"]': 'Heat',
'["Not","Heat"]': 'Cold',
2024-11-26 17:07:42 -05:00
'["Crystal","Cold"]': 'Rock',
2024-11-26 19:25:45 -05:00
'["Heat","Rock"]': 'Lava',
'["Heat","Lava"]': 'Plasma',
'["Heat","Plasma"]': 'Star',
'["Rock","Star"]': 'Planet',
'["Void","Rock"]': 'Air',
'["Air","Energy"]': 'Wind',
2024-11-26 17:07:42 -05:00
'["Rock","Wind"]': 'Stone',
'["Stone","Wind"]': 'Cobblestone',
2024-11-26 19:25:45 -05:00
'["Cobblestone","Wind"]': 'Sand',
2024-11-26 17:07:42 -05:00
'["Cobblestone","Sand"]': 'Gravel',
2024-11-26 19:25:45 -05:00
'["Star","Chemical"]': 'Hydrogen',
'["Air","Hydrogen"]': 'Water',
'["Water","Void"]': 'Ocean',
'["Energy","Ocean"]': 'Life',
2024-11-26 17:07:42 -05:00
'["Gravel","Life"]': 'Dirt',
}
const compCache = {};
const deCompCache = {};
2024-11-26 19:25:45 -05:00
function indexr(n, log_mode = false) {
2024-11-26 17:07:42 -05:00
let j;
try {
j = JSON.parse(Object.keys(elems)[Object.values(elems).findIndex(x => x == n)]);
} catch (err) {
2024-11-26 19:25:45 -05:00
return (log_mode) ? 1 : 0n;
2024-11-26 17:07:42 -05:00
}
2024-11-26 19:25:45 -05:00
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';
2024-11-26 17:07:42 -05:00
}
2024-11-26 19:25:45 -05:00
function comp(e, ignore = false, log_mode = false) {
if (!e || typeof e != 'object' || e.length == 0) return (log_mode) ? 0 : 0n;
2024-11-26 17:07:42 -05:00
let a, b;
if (ignore) {
a = e[0];
b = e[1];
} else {
let [j, k] = e;
2024-11-26 19:25:45 -05:00
a = compCache[`${j}.${log_mode}`] || indexr(j, log_mode);
compCache[`${j}.${log_mode}`] = a;
2024-11-26 17:07:42 -05:00
2024-11-26 19:25:45 -05:00
b = compCache[`${k}.${log_mode}`] || indexr(k, log_mode);
compCache[`${k}.${log_mode}`] = b;
2024-11-26 17:07:42 -05:00
}
if (b > a) {
2024-11-26 19:25:45 -05:00
return compSub(a,b,log_mode);
} else if (a != 'oh no' && b != 'oh no') {
return compSub(b,a,log_mode);
2024-11-26 17:07:42 -05:00
} else {
2024-11-26 19:25:45 -05:00
return 'oh no';
2024-11-26 17:07:42 -05:00
}
}
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;
2024-11-26 19:25:45 -05:00
let out = JSON.stringify([h2, s2]);
2024-11-26 17:07:42 -05:00
if (elems[out]) return elems[out];
2024-11-26 19:25:45 -05:00
out = JSON.stringify([s2, h2]);
2024-11-26 17:07:42 -05:00
if (elems[out]) return elems[out];
2024-11-26 19:25:45 -05:00
return [h2, s2]
2024-11-26 17:07:42 -05:00
}
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) {
2024-11-26 19:25:45 -05:00
let compd;
try {
compd = indexr(elems[i]).toString(10);
} catch (err) {
}
if (compd == 'oh no') {
compd = `10^(${indexr(elems[i],true)})`;
}
2024-11-26 17:07:42 -05:00
compd += '';
let l = compd.length;
2024-11-26 19:25:45 -05:00
//compd = compd.slice(0, Math.min(l, 100));
2024-11-26 17:07:42 -05:00
2024-11-26 19:25:45 -05:00
console.log(`${elems[i]}: ${compd} (${l} chars)\n`)
2024-11-26 17:07:42 -05:00
}