728x90
๐ section04 - 2 - ๋ค์ง์ ์์(bruteForce)
์์ฐ์๋ฅผ ๋ค์ง์ ๋ string
ํ์ ์ฌ์ฉํ๋ ๋์ number
ํ ๊ทธ๋๋ก ๋ค์ง๋ ๋ฐฉ๋ฒ์ ์๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค. ๊ทธ๋ฆฌ๊ณ ์์๋ฅผ ํ๋ณํ ๋๋ ๋ฐ๋ณต๋ฌธ์ ๋ฒ์๋ฅผ i <=Math.floor(n**0.5)๊น์ง๋ง
์ค์ ํด์ฃผ๋ฉด ๋ ์ ์ ์๊ฐ์ ์์๋ฅผ ํ๋ณํ ์ ์๋ค. ์ฝ๋์ ๊ฐ๋
์ฑ์ ๋์ด๊ธฐ ์ํด ์์ฐ์๋ฅผ ๋ค์ง๋ ๋ก์ง๊ณผ ์์๋ฅผ ํ๋ณํ๋ ๋ก์ง์ ๋๋ ์ ํ๋ ํธ๋ ์ข๋ค. ์์ ํ๋ณ ๋ฌธ์ ๋ ์ด์ ์๋ ์ข
์ข
ํ์๋ค.
// ๋์ ์ฝ๋
let n = 9;
let arr = [32, 55, 62, 20, 250, 370, 200, 30, 100];
console.log(solution(n, arr));
function solution(n, arr) {
let answer = "";
for (let number of arr) {
let sum = 0;
let flag = true;
do {
sum = sum * 10 + number % 10;
number = Math.floor(number / 10);
} while (number > 1);
if (sum > 1) {
for (let i = 2; i <= Math.floor(sum ** 0.5); i++) {
if (sum % i === 0) {
flag = false;
break;
}
}
if (flag) answer += `${sum} `;
}
}
return answer.slice(0, -1);
}
728x90
// ๊ฐ์ฌ๋ ์ฝ๋
let n = 9;
let arr = [32, 55, 62, 20, 250, 370, 200, 30, 100];
console.log(solution(n, arr));
function isPrime(n) {
if (n < 2) return false;
for (let i = 2; i <= Math.floor(n ** 0.5); i++) {
if (n % i === 0) return false;
}
return true;
}
function solution(n, arr) {
let ans = "";
for (let number of arr) {
let sum = 0;
do {
sum = sum * 10 + (number % 10);
number = Math.floor(number / 10);
} while (number > 1);
if (isPrime(sum)) ans += `${sum} `;
}
return ans.slice(0, -1);
}
๋ฐ์ํ
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] section04 - 4 - ์กธ์ ์ ๋ฌผ (0) | 2021.08.22 |
---|---|
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] section04 - 3 - ๋ฉํ ๋ง (0) | 2021.08.22 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] section04 - 1 - ์๋ฆฟ์์ ํฉ (0) | 2021.08.22 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 28 - ๋ฌธ์์ด ์์ถ (0) | 2021.08.17 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 27 - ๊ฐ์ฅ ์งง์ ๋ฌธ์๊ฑฐ๋ฆฌ (0) | 2021.08.17 |
๋๊ธ