我正在尝试制作更节省内存的素数生成器。我不能想到比使用文件作为我的素数数组更好的东西,并且可以同步逐行读取。我运行它并在大约29000它给我ENFILE: File table overflow
错误。有任何修改/改进此代码的想法吗?
#!/usr/bin/env node
var num = 3,
prime,
fs = require("fs"),
readline = require("./line_reader.js");
console.log("Hundreds: 2");
console.log("Hundreds: 3");
function appendPrime (num) {
fs.appendFileSync("Primes.txt", num+'\n');
}
while (true) {
prime = true;
var times = Math.floor(Math.pow(num, 0.5));
if (num % 2 == 0 || num % 3 == 0) {
prime = false;
} else {
readline.eachLine("Primes.txt", function (line, last, cb) {
if (num % line == 0) {
prime = false;
cb(true);
} else {
cb();
}
});
}
if (prime) {
var place = String(num).length;
switch (true) {
case place <= 3:
console.log("Hundreds: "+num);
break;
case place <= 5 :
console.log("Thousands: "+num);
break;
case 6 :
console.log("Hundred thousands: "+num);
break;
case place <= 8 :
console.log("Millions: "+num);
break;
case 9 :
console.log("Hundred millions: "+num);
break;
case place >= 10 :
console.log("Billions or above: "+num);
break;
}
appendPrime(num);
}
num += 2;
}
答案 0 :(得分:0)
试试这个
sudo launchctl limit maxfiles 16384 16384 && ulimit -n 16384