Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,5 @@
k = 26
s = k.toString(16) //gives 1a
i = parseInt('1a',16) //gives 26
//optional special case for hex:
i = +('0x'+s) //hexadecimal base 16, if s='1a' then i=26.

View file

@ -0,0 +1,19 @@
var baselist = "0123456789abcdefghijklmnopqrstuvwxyz", listbase = [];
for(var i = 0; i < baselist.length; i++) listbase[baselist[i]] = i; // Generate baselist reverse
function basechange(snumber, frombase, tobase)
{
var i, t, to = new Array(Math.ceil(snumber.length * Math.log(frombase) / Math.log(tobase))), accumulator;
if(1 < frombase < baselist.length || 1 < tobase < baselist.length) console.error("Invalid or unsupported base!");
while(snumber[0] == baselist[0] && snumber.length > 1) snumber = snumber.substr(1); // Remove leading zeros character
console.log("Number is", snumber, "in base", frombase, "to base", tobase, "result should be",
parseInt(snumber, frombase).toString(tobase));
for(i = snumber.length - 1, inexp = 1; i > -1; i--, inexp *= frombase)
for(accumulator = listbase[snumber[i]] * inexp, t = to.length - 1; accumulator > 0 || t >= 0; t--)
{
accumulator += listbase[to[t] || 0];
to[t] = baselist[(accumulator % tobase) || 0];
accumulator = Math.floor(accumulator / tobase);
}
return to.join('');
}
console.log("Result:", basechange("zzzzzzzzzz", 36, 10));

View file

@ -0,0 +1,28 @@
// Tom Wu jsbn.js http://www-cs-students.stanford.edu/~tjw/jsbn/
var baselist = "0123456789abcdefghijklmnopqrstuvwxyz", listbase = [];
for(var i = 0; i < baselist.length; i++) listbase[baselist[i]] = i; // Generate baselist reverse
function baseconvert(snumber, frombase, tobase) // String number in base X to string number in base Y, arbitrary length, base
{
var i, t, to, accum = new BigInteger(), inexp = new BigInteger('1', 10), tb = new BigInteger(),
fb = new BigInteger(), tmp = new BigInteger();
console.log("Number is", snumber, "in base", frombase, "to base", tobase, "result should be",
frombase < 37 && tobase < 37 ? parseInt(snumber, frombase).toString(tobase) : 'too large');
while(snumber[0] == baselist[0] && snumber.length > 1) snumber = snumber.substr(1); // Remove leading zeros
tb.fromInt(tobase);
fb.fromInt(frombase);
for(i = snumber.length - 1, to = new Array(Math.ceil(snumber.length * Math.log(frombase) / Math.log(tobase))); i > -1; i--)
{
accum = inexp.clone();
accum.dMultiply(listbase[snumber[i]]);
for(t = to.length - 1; accum.compareTo(BigInteger.ZERO) > 0 || t >= 0; t--)
{
tmp.fromInt(listbase[to[t]] || 0);
accum = accum.add(tmp);
to[t] = baselist[accum.mod(tb).intValue()];
accum = accum.divide(tb);
}
inexp = inexp.multiply(fb);
}
while(to[0] == baselist[0] && to.length > 1) to = to.slice(1); // Remove leading zeros
return to.join('');
}

View file

@ -0,0 +1,93 @@
(() => {
'use strict';
// toBase :: Int -> Int -> String
const toBase = (intBase, n) =>
intBase < 36 && intBase > 0 ?
inBaseDigits('0123456789abcdef'.substr(0, intBase), n) : [];
// inBaseDigits :: String -> Int -> [String]
const inBaseDigits = (digits, n) => {
const intBase = digits.length;
return unfoldr(maybeResidue => {
const [divided, remainder] = quotRem(maybeResidue.new, intBase);
return {
valid: divided > 0,
value: digits[remainder],
new: divided
};
}, n)
.reverse()
.join('');
};
// GENERIC FUNCTIONS
// unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
const unfoldr = (mf, v) => {
var xs = [];
return (until(
m => !m.valid,
m => {
const m2 = mf(m);
return (
xs = xs.concat(m2.value),
m2
);
}, {
valid: true,
value: v,
new: v,
}
), xs);
};
// curry :: ((a, b) -> c) -> a -> b -> c
const curry = f => a => b => f(a, b);
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = (p, f, x) => {
let v = x;
while (!p(v)) v = f(v);
return v;
}
// quotRem :: Integral a => a -> a -> (a, a)
const quotRem = (m, n) => [Math.floor(m / n), m % n];
// show :: a -> String
const show = x => JSON.stringify(x, null, 2);
// OTHER FUNCTIONS DERIVABLE FROM inBaseDigits
// inLowerHex :: Int -> String
const inLowerHex = curry(inBaseDigits)('0123456789abcdef');
/// inUpperHex :: Int -> String
const inUpperHex = curry(inBaseDigits)('0123456789ABCDEF');
// inOctal :: Int -> String
const inOctal = curry(inBaseDigits)('01234567');
// inDevanagariDecimal :: Int -> String
const inDevanagariDecimal = curry(inBaseDigits)('०१२३४५६७८९');
// TESTS
// testNumber :: [Int]
const testNumbers = [255, 240];
return testNumbers.map(n => show({
binary: toBase(2, n),
base5: toBase(5, n),
hex: toBase(16, n),
upperHex: inUpperHex(n),
octal: inOctal(n),
devanagariDecimal: inDevanagariDecimal(n)
}));
})();