Created
January 8, 2017 07:51
-
-
Save breakstorm/936febd4eebe055ac0da3d46b9b0a91a to your computer and use it in GitHub Desktop.
sosu
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
// 에라토스테네스의 체 | |
// N배열을 생성을 한다. | |
// 1을 삭제, | |
// 2를 제외한 2의 배수를 지움 | |
// 3을 제외한 3의 배수 지움 | |
// 4는 없어서 패스 | |
// 5는 제외하고 5의 배수 지움 | |
// 반복 | |
function sosu_arr(num){ | |
var myArr = new Array() | |
for(i=0; i < num+1; i++){ | |
myArr.push(i) | |
} | |
return myArr | |
} | |
function sosu_chk(myArr,num){ | |
//1~num까지 배수를 생성하여 숫자를 배열에서 제외 | |
myArr.shift();//0제외 | |
myArr.shift();//1제외 | |
last = myArr.length; | |
for(i=0; i < myArr.length; i++){ | |
for(j=2; j < parseInt(num/2); j++){ | |
if(myArr[i]*j > num) continue; | |
temp = myArr.indexOf(myArr[i]*j); | |
if(temp === -1) continue; | |
myArr.splice(temp,1); | |
} | |
} | |
myArr.shift(); | |
return myArr; | |
} | |
function sosu(num){ | |
//배열생성 | |
var arr1 = sosu_arr(num) | |
//숫자제외 | |
sosu_chk(arr1,num) | |
return arr1 | |
} | |
console.log(sosu(10)) | |
sosu(10) | |
console.log(sosu(100)) | |
sosu(1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment