Skip to content

Commit

Permalink
07.15: prime number, integer(수 나누기 게임)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Jul 15, 2024
1 parent ccaf748 commit 4c46b21
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions baekjoon/27172.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 수 나누기 게임 : 소수, 에라토스테네스의 체, 정수론
const input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n")
.map((v) => v.split(" ").map((v) => Number(v)));
const [n] = input.shift();
const cards = input.shift();
const biggest = cards
.slice()
.sort((a, b) => a - b)
.at(-1);
const count = new Array(biggest + 1).fill(0);
const isValid = new Array(biggest + 1).fill(null);
const answer = [];

for (const number of cards) {
const yaksu = [];
const sqrt = Math.sqrt(number);
for (let i = 1; i <= sqrt; i++) {
if (number % i === 0) {
yaksu.push(i);
count[i]++;

if (number / i !== i) {
yaksu.push(number / i);
count[number / i]++;
}
}
}
isValid[number] = yaksu;
}

for (let i = 0; i < cards.length; i++) {
const number = cards[i];
const yaksus = isValid[number].sort((a, b) => a - b);

let cnt = 0;
for (const yaksu of yaksus) {
if (isValid[yaksu]) {
cnt++;
}
}
answer.push(count[number] - cnt);
}

console.log(answer.join(" "));

0 comments on commit 4c46b21

Please sign in to comment.