elem-test/test.js

130 lines
3 KiB
JavaScript
Raw Normal View History

2024-11-26 17:07:42 -05:00
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',
'["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',
'["Binary","Operator"]': 'Function',
'["Function","Zero"]': 'Algebra',
'["Algebra","Calculus"]': 'Mathematics',
'["Mathematics","Deabstraction"]': 'Science',
'["Science","Combination"]': 'Chemistry',
'["Chemistry","Object"]': 'Chemical',
'["Chemical","Pattern"]': 'Crystal',
'["Crystal","Cold"]': 'Rock',
'["Rock","Wind"]': 'Stone',
'["Stone","Wind"]': 'Cobblestone',
'["Cobblestone","Sand"]': 'Gravel',
'["Gravel","Life"]': 'Dirt',
}
const compCache = {};
const deCompCache = {};
function indexr(n) {
let j;
try {
j = JSON.parse(Object.keys(elems)[Object.values(elems).findIndex(x => x == n)]);
} catch (err) {
return 0n;
}
return comp(j);
}
function comp(e, ignore = false) {
if (!e || typeof e != 'object' || e.length == 0) return 0n;
let a, b;
if (ignore) {
a = e[0];
b = e[1];
} else {
let [j, k] = e;
a = compCache[j] || indexr(j);
compCache[j] = a;
b = compCache[k] || indexr(k);
compCache[k] = b;
}
if (b > a) {
return (b * b + b) / 2n + a + 1n;
} else {
return (a * a + a) / 2n + b + 1n;
}
}
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 = indexr(elems[i]);
compd += '';
let l = compd.length;
compd = compd.slice(0,Math.min(l,10));
console.log(`${elems[i]}: ${compd}... (${l} digits)`)
}