This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SumBigNumbers (a, b) { | |
var res = '', c = 0; | |
a = a.split(''); | |
b = b.split(''); | |
while (a.length || b.length || c) { | |
c += ~~a.pop() + ~~b.pop(); | |
res = c % 10 + res; | |
c = c > 9; | |
} | |
return res; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hammingWeight = v => { | |
v = v - (v>>1 & 0x55555555); | |
v = (v & 0x33333333) + (v>>2 & 0x33333333); | |
return ((v + (v>>4) & 0xF0F0F0F) * 0x1010101) >> 24; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Extend the String object with toBase64() and fromBase64() functions | |
String.prototype.toBase64 = function () { | |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; | |
let result = ''; | |
let i = 0; | |
while (i < this.length) { | |
let a = this.charCodeAt(i++) || 0; | |
let b = this.charCodeAt(i++) || 0; | |
let c = this.charCodeAt(i++) || 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add() { | |
var c = 1000000000; | |
var args = Array.prototype.slice.call(arguments); | |
var sum = 0; | |
for (var i = 0; i < args.length; i++) sum += args[i] * c; | |
return (sum / c).toString(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toChineseNumeral(num) { | |
var numerals = { | |
"-":"负", | |
".":"点", | |
0:"零", | |
1:"一", | |
2:"二", | |
3:"三", | |
4:"四", | |
5:"五", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const RomanNumerals = { | |
toRoman: v => { | |
let s = ''; | |
RomanNumerals.numerals.forEach(n => { | |
while (v >= n[1]) { | |
s += n[0]; | |
v -= n[1]; | |
} | |
}); | |
return s; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parseInt = string => { | |
var n = 0, | |
g = 0; | |
function text2num(s) { | |
const a = s.toString().split(/[\s-]+/); | |
a.forEach(feach); | |
return n + g; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const determinant = m => { | |
const l = m.length; | |
if (l === 1) return m[0][0]; | |
if (l === 2) return m[0][0] * m[1][1] - m[1][0] * m[0][1]; | |
let result = 0; | |
for (let i = 0; i < l; i++) { | |
result += Math.pow(-1, i) * m[0][i] * determinant(minor(m, i, 0)); | |
} | |
return result; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const regexDivisibleBy = n => { | |
if (n == 1) return '^(0|1)+$'; | |
if (n == 2) return '^(0|1)+0$'; | |
if (n == 3) return '^(0*(1(01*0)*1)*)*$'; | |
if (n == 4) return '^(0|1)+00$'; | |
if (n == 5) return '^(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))*1)+$'; | |
if (n == 6) return '^(0|(11|1(00|011*0)(00|011*0)*1)(11|1(00|011*0)(00|011*0)*1)*0)+$'; | |
if (n == 7) return '^(0|(10((0|11)(1|00))*(10|(0|11)01)|11)(01*0(0|101|1(1|00)((0|11)(1|00))*(10|(0|11)01)))*1)+$'; | |
if (n == 8) return '^(0|1)+000$'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function regexDivisibleBy(n) { | |
if (n==1) return '^[01]+$' // Special case | |
let a = [] // List of nodes | |
for (let i=0; i<n; i++) a[i] = {} // Node | |
for (let i=0; i<n; i++) { | |
a[i].id = i // Used for indentification below | |
a[i][i*2%n] = '0' // Keys in nodes are id refs to other nodes | |
a[i][(i*2+1)%n] = '1' // Values in nodes are the required chars to get to next node (the key) | |
} | |
OlderNewer