Last active
August 30, 2017 12:04
-
-
Save ApsarasX/56554264e009a0776f2c796983d9a677 to your computer and use it in GitHub Desktop.
About Prim(关于质数的一些操作)
This file contains 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 generatePrimesList(num) { | |
let ret = []; | |
if (num >= 2) | |
ret.push(2); | |
for (let i = 3; i <= num; i++) { | |
let flag = true; | |
for (let j = 0; j < ret.length; j++) { | |
if (i % ret[j] === 0) { | |
flag = false; | |
break; | |
} | |
} | |
if (flag) | |
ret.push(i); | |
} | |
return ret; | |
} | |
function generatePrimesList2(...nums) { | |
let [num1, num2] = nums.sort((a, b) => a - b); | |
let ret1 = generatePrimesList(num1); | |
let ret2 = generatePrimesList(num2); | |
let start = ret1.includes(num1) ? ret1.length - 1 : ret1.length; | |
return ret2.slice(start); | |
} |
This file contains 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 isPrime(num) { | |
if(num<=1) return false; | |
let flag = true; | |
for(let i=2;i<=Math.ceil(Math.sqrt(num));i++) { | |
if(num%i===0 && num!==i) { | |
flag = false; | |
break; | |
} | |
} | |
return flag; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment