-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththericebet.js
102 lines (97 loc) · 3.14 KB
/
thericebet.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function main() {
var number = document.getElementById('number');
var daysSinceElem = document.getElementById('daysSince');
var today = new Date();
var dayZero = new Date(2016, 6, 6); // I think it happened at about 9 o clock on the 6 July 2016. Months are indexed from 0 for some reason.
var oneDay = 1000 * 60 * 60 * 24;
var daysSince = ((today.getTime() - dayZero.getTime()) / oneDay)| 0;
daysSinceElem.appendChild(document.createTextNode(daysSince));
number.appendChild(document.createTextNode(spaceDigits(pow2(daysSince).toString(), 3)));
};
function spaceDigits(s, n) {
var a = '';
var count = 0;
for (var i = s.length - 1; i >= 0; --i) {
if (count == n) {
a = ' ' + a;
count = 0;
}
a = s.charAt(i) + a;
count++;
}
return a;
}
function pow2(n) {
var a = BigNum(1);
for (var i = 0; i < n; ++i) {
a.add(a.copy());
}
return a;
}
// It's 15 so that two of them will fit in an int32 (easier for division)
var BigNumBits = 15; // Number of bits in a big num
function BigNum (n) {
/*A little endian positive big num*/
var mask = (1 << BigNumBits) - 1;
var that = {
digits: [n || 0],
add: function (n) {
var carry = 0;
for (var i = 0; i < n.digits.length; ++i) {
if (i >= that.digits.length) {
that.digits.push(0);
}
that.digits[i] += n.digits[i] + carry;
if (that.digits[i] > mask) {
carry = 1;
} else {
carry = 0;
}
that.digits[i] &= mask;
}
if (carry == 1) {
that.digits.push(1);
}
},
divSmall: function(n) {
var result = BigNum();
result.digits = [];
n = n | 0; // force n to be an int32
var broughtDown = 0;
for (var i = that.digits.length - 1; i >= 0; i--) {
var dividend = (broughtDown << BigNumBits) + that.digits[i];
var rem = dividend % n;
var quotient = (dividend / n) | 0;
result.digits.push(quotient);
broughtDown = rem;
}
result.digits.reverse();
return {quot: result, rem: broughtDown};
},
copy: function (n) {
var n = BigNum(0);
n.add(that);
return n;
},
isZero: function (n) {
for (var i = 0; i < that.digits.length; ++i) {
if (that.digits[i] != 0) return false;
}
return true;
},
toString: function() {
var n = that;
var result = "";
var digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
while (!n.isZero()) {
var div = n.divSmall(10);
result = digits[div.rem] + result;
n = div.quot;
}
return result;
}
};
that.prototype = Object;
that.prototype.toString = that.toString;
return that;
}