Link
Notice
HIT해
[JS 백준] 1929.소수 구하기 본문
728x90
// 소수 구하기
// 3 16
const filePath = process.platform === 'linux' ? '/dev/stdin' : 'input.txt';
const input = require("fs")
.readFileSync("input.txt")
.toString()
.trim()
.split(" ").map(Number);
// console.log("input = " + input)
let num1 = input.shift();
let num2 = input.shift();
for(let i = num1; i <= num2; i++){
if (i === 1) continue; // 1은 소수가 아니므로 건너뛰기
let flag = false;
for(let j = 2; j * j <= i; j++){ // j는 i의 제곱근 이하까지만 확인
if(i % j == 0){
flag = true;
break;
}
};
if(flag == false){
console.log(i);
}
}
점점 익숙해지는 것 같다.
'Vue > JavaScript 알고리즘' 카테고리의 다른 글
[JS 백준] 28279.덱 2 (0) | 2024.01.07 |
---|---|
[JS 백준] 9012.괄호 (0) | 2024.01.07 |
[JS 백준] 1978.소수찾기 (0) | 2024.01.05 |
[JS 백준] 10798.세로읽기 (0) | 2024.01.05 |
[JS 백준] 2443 단어 길이 재기 (1) | 2024.01.04 |