blob: 063c8aae7e145f850fb7189dc62ba86a21144829 (
plain)
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
|
const alph = "0123456789abcdefghijklmnopqrstuvwxy";
// input a base-35 number (a string), output base-10
const from35 = function(s) {
let n = s.length - 1;
let sum = 0;
for (let d of s) {
sum += alph.indexOf(d) * Math.pow(35,n);
n--;
}
return sum;
};
// input base-10, output base-35
const to35 = function(d) {
let rem = parseInt(d % "35");
let n = 0;
while (true) {
let q = parseInt(d / String(Math.pow(35, n+1)));
if ( q < 1 ) {
break;
}
n++;
}
let coeffs = [];
for (let k = n; k > 0; k--) {
coeffs.push( Math.floor(d / Math.pow(35, k)) );
}
coeffs.push(rem);
let s = "";
for (let c of coeffs) {
s += alph.charAt(c);
}
return s;
};
// 2u <- 2(35) + 30 = 100
let num = "1000";
console.log("Input: (base-10) ", num);
console.log("Output: (base-35) ", to35(num));
// 120 = 105 + 15 -> 3(35) + 15
let x = "3f";
console.log("Input: (base-35) ", x);
console.log("Output: (base-10)", from35(x));
// // // // // // // // // // // //
// Proper way (would work for bases up to 36):
// function to35(d) {
// return d.toString(35);
// }
// function to10(s) {
// return parseInt(s, 35);
// }
// example
// let d = 100;
// let x = d.toString(35); // a base-35 number
// console.log(x); // 2u
// console.log(parseInt(x, 35)); // 100 base-35 -> base-10
|