JavaScript 中的类型化数组与正则表达式
类型化数组
类型化数组是 JavaScript 中处理二进制数据的强大工具。创建类型化数组后,可使用常规的方括号表示法读写其元素,就像处理其他类数组对象一样。
// Return the largest prime smaller than n, using the sieve of Eratosthenes
function sieve(n) {
let a = new Uint8Array(n+1); // a[x] will be 1 if x is composite
let max = Math.floor(Math.sqrt(n)); // Don't do factors higher than this
let p = 2; // 2 is the first prime
while(p <= max) { // For primes less than max
for(let i = 2*p; i <= n; i += p) // Mark multiples of p as composite
a[i] = 1;
while(a[++p]) /* empty */; // The next unmarked index is prime
}
while(a[n]) n--; // Loop
超级会员免费看
订阅专栏 解锁全文
1199

被折叠的 条评论
为什么被折叠?



