质数,只能被自己和1整除的整数。
判断要点1:质数数组默认为[2], n若能被任意质数数组中的值整除,则不是质数
(function displayPrime() { let primeArray = [2]; for (let i=3; i< 10000; i++) { let iIsPrime = true; for (let prime of primeArray) { if (i % prime === 0) { iIsPrime = false; break; } } if (iIsPrime) { primeArray.push(i); } } console.info(primeArray); })();
判断要点2:偶数一定不是质数
(function displayPrime() { let time1=(new Date()).valueOf(); let primeArray = [2]; for (let i=3; i< 10000; i+=2) { let iIsPrime = true; for (let prime of primeArray) { if (i % prime === 0) { iIsPrime = false; break; } } if (iIsPrime) { primeArray.push(i); } } console.log('timeout: '+ ((new Date()).valueOf() - time1)) console.info(primeArray); }());
判断要点3:开平方的结果是整数的整数不是质数
(function displayPrime() { let time1=(new Date()).valueOf(); let primeArray = [2]; for (let i=3; i< 10000; i+=2) { let sqrt=Math.sqrt(i), borderNum = i%sqrt; let iIsPrime = !(borderNum === 0 ); if (!iIsPrime) { continue; } for (let prime of primeArray) { if (i % prime === 0) { iIsPrime = false; break; } } if (iIsPrime) { primeArray.push(i); } } console.log('timeout: '+ ((new Date()).valueOf() - time1)) console.info(primeArray); }());