일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 백준
- codesandbox
- react hook
- 컴퓨터 네트워크
- vanillaJS
- design pattern
- Java
- 코딩테스트 고득점 Kit
- 코딩테스트 고득점 Kit 완전탐색
- JavaScript
- websocket
- 프로그래머스 완전탐색
- useState
- NextJS
- React JS
- 데이터모델링과마이닝
- react firebase
- 프로그래머스
- 리액트 훅
- 디자인 패턴
- 자바
- 자바스크립트
- 장고
- 코틀린
- 리액트
- react
- 프로그래밍 언어론
- 프로그래머스 자바
- useEffect
- 자바 공부
Archives
- Today
- Total
기록하는 개발자
[프로그래머스][코딩테스트 고득점 Kit][Javascript] 소수 찾기 본문
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42839?language=javascript#
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
전체코드
function solution(numbers) {
let answer = [];
let numArr = numbers.split("");
// fixedNum : 순열을 통해 모든 조합을 생성할 때 고정할 숫자값
// ex) 134의 경우
// i) fixedNum=1 -> 13, 14의 소수 여부 검사
// ii) fixedNum=13 -> 134의 소수 여부 검사
function getPermutationArr(numArr, fixedNum){
if(numArr.length==0) return;
for(let i=0; i<numArr.length; i++) {
const newNum = fixedNum + numArr[i];
const copyArr = [...numArr];
// splice를 통해 index i 번째 요소 삭제
copyArr.splice(i, 1);
// newNum : 01 +newNum : 1
// answer 배열 내 newNum이 없고, newNum이 소수인 경우
if (!answer.includes(+newNum) && isPrime(+newNum)){
answer.push(+newNum)
}
getPermutationArr(copyArr, newNum);
}
}
function isPrime(num){
if(num<=1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
getPermutationArr(numArr,"")
return answer.length;
}
728x90
'Algorithm' 카테고리의 다른 글
[프로그래머스][코딩테스트 고득점 Kit][Javascript] 정수 삼각형 (0) | 2023.08.13 |
---|---|
[프로그래머스][코딩테스트 고득점 Kit][Javascript] 모의고사 (0) | 2023.08.13 |
[프로그래머스][코딩테스트 고득점 Kit][Javascript] 더 맵게 (0) | 2023.08.10 |
[프로그래머스][코딩테스트 고득점 Kit][Javascript] N으로 표현 (0) | 2023.08.10 |
[프로그래머스][코딩테스트 고득점 Kit][Javascript] 올바른 괄호 (0) | 2023.08.10 |