๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ์ธ๊ณต๋ถ€/Algorithm

[๋ฐฑ์ค€][Node.js] 6359๋ฒˆ : ๋งŒ์ทจํ•œ ์ƒ๋ฒ”

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 12. 3.

 

 

Algorithm

-  ๋งŒ์ทจํ•œ ์ƒ๋ฒ” -

 


 

๋ฌธ์ œ

์„œ๊ฐ•๋Œ€ํ•™๊ต ๊ณค์ž๊ฐ€ ๊ธฐ์ˆ™์‚ฌ์˜ ์ง€ํ•˜์—๋Š” n๊ฐœ์˜ ๋ฐฉ์ด ์ผ๋ ฌ๋กœ ๋Š˜์–ด์„  ๊ฐ์˜ฅ์ด ์žˆ๋‹ค. ๊ฐ ๋ฐฉ์—๋Š” ๋ฒŒ์ ์„ ๋งŽ์ด ๋ฐ›์€ ํ•™์ƒ์ด ๊ตฌ๊ธˆ๋˜์–ด์žˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋˜ ์–ด๋Š ๋‚ , ๊ฐ์˜ฅ ๊ฐ„์ˆ˜์ธ ์ƒ๋ฒ”์ด๋Š” ์ง€๋ฃจํ•œ ๋‚˜๋จธ์ง€ ์ •์‹ ๋‚˜๊ฐ„ ๊ฒŒ์ž„์„ ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ๋‹ค. ๊ฒŒ์ž„์˜ ์ฒซ ๋ฒˆ์งธ ๋ผ์šด๋“œ์—์„œ ์ƒ๋ฒ”์ด๋Š” ์œ„์Šคํ‚ค๋ฅผ ํ•œ ์ž” ๋“ค์ดํ‚ค๊ณ , ๋‹ฌ๋ ค๊ฐ€๋ฉฐ ๊ฐ์˜ฅ์„ ํ•œ ๊ฐœ์”ฉ ๋ชจ๋‘ ์—ฐ๋‹ค. ๊ทธ ๋‹ค์Œ ๋ผ์šด๋“œ์—์„œ๋Š” 2, 4, 6, ... ๋ฒˆ ๋ฐฉ์„ ๋‹ค์‹œ ์ž ๊ทธ๊ณ , ์„ธ ๋ฒˆ์งธ ๋ผ์šด๋“œ์—์„œ๋Š” 3, 6, 9, ... ๋ฒˆ ๋ฐฉ์ด ์—ด๋ ค์žˆ์œผ๋ฉด ์ž ๊ทธ๊ณ , ์ž ๊ฒจ์žˆ๋‹ค๋ฉด ์—ฐ๋‹ค. k๋ฒˆ์งธ ๋ผ์šด๋“œ์—์„œ๋Š” ๋ฒˆํ˜ธ๊ฐ€ k์˜ ๋ฐฐ์ˆ˜์ธ ๋ฐฉ์ด ์—ด๋ ค ์žˆ์œผ๋ฉด ์ž ๊ทธ๊ณ , ์ž ๊ฒจ ์žˆ๋‹ค๋ฉด ์—ฐ๋‹ค. ์ด๋ ‡๊ฒŒ n๋ฒˆ์งธ ๋ผ์šด๋“œ๊นŒ์ง€ ์ง„ํ–‰ํ•œ ์ดํ›„, ์ƒ๋ฒ”์ด๋Š” ์œ„์Šคํ‚ค์˜ ๋งˆ์ง€๋ง‰ ๋ณ‘์„ ๋งˆ์‹œ๊ณ  ์“ฐ๋Ÿฌ์ ธ ์ž ๋“ ๋‹ค.

 

๊ตฌ๊ธˆ๋˜์–ด์žˆ๋Š” ๋ช‡ ๋ช…(์–ด์ฉŒ๋ฉด 0๋ช…)์˜ ํ•™์ƒ๋“ค์€ ์ž์‹ ์˜ ๋ฐฉ์„ ์ž ๊ทธ์ง€ ์•Š์€ ์ฑ„ ์ƒ๋ฒ”์ด๊ฐ€ ์“ฐ๋Ÿฌ์ ธ๋ฒ„๋ ธ๋‹จ ๊ฒƒ์„ ๊นจ๋‹ซ๊ณ  ์ฆ‰์‹œ ๋„๋ง์นœ๋‹ค.

 

๋ฐฉ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ช‡ ๋ช…์˜ ํ•™์ƒ๋“ค์ด ๋„์ฃผํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด์ž.

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

๋ฌธ์ œ ํ’€๊ธฐ

const readline = require("readline");
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
	input.push(+line.toString());
}).on("close", function () {
	input.shift();
	let result = [];

	for (let i = 0; i < input.length; i++) {
		// prison 0๋ฒˆ์งธ๋Š” index ๋งž์ถ”๊ธฐ
		let prison = new Array(input[i] + 1).fill(true);
		for (let j = 2; j <= input[i]; j++) {
			for (let k = j; k <= input[i]; k += j) {
				prison[k] = !prison[k];
			}
		}
		result.push(prison.filter((el) => el === true).length - 1);
	}
	console.log(result.join("\n"));
	process.exit();
});

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€