Skip to content

소수 찾기 #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2022
Merged

소수 찾기 #77

merged 1 commit into from
Jul 2, 2022

Conversation

NamJwong
Copy link
Owner

@NamJwong NamJwong commented Jul 1, 2022

문제

풀이 후기

순열 구하는 코드랑 소수 찾는 코드를 가지고 있어서 쉽게 풀었던.. 소수 찾는 코드는 이번 기회에 약간 보완했다.
근데 아마 훨씬 간단한 코드가 있을 것만 같다.

다른 사람의 풀이와 비교

좋아요 1등 풀이

function solution(numbers) {
    var answer = 0;

    var n = numbers.split('');
    var nums = new Set()
    combi(n,'');

    function combi(a, s) {
        if (s.length > 0) {
            if (nums.has(Number(s))=== false) {
                nums.add(Number(s));
            console.log(Number(s))
                if (chkPrime(Number(s))) {

                    answer++;
                }
            }
        }
        if (a.length > 0) {
            for (var i = 0; i< a.length; i++) {
                var t = a.slice(0)
                t.splice(i,1);
                //console.log(t)
                combi(t,s + a[i]);
            }
        }
    }

    function chkPrime(num) {
        if (num < 2) return false;
        if (num === 2) return true;
        for (var i = 2; i <= Math.sqrt(num); i++) {
            if (num%i===0) return false;
        }
        return true;
    }

    return answer;
}

순열 만드는 부분을 재귀로 짰는데 어차피 난 이렇게까지 복잡한 재귀 코드 지금 이해한다고 내일 바로 적용할 수 있는 거 아니기 때문에 패스하고 다음에 다시 보겠다 ㅎㅎ .. 대신 다른 문제를 하나 더 풀어야지. .

@NamJwong NamJwong self-assigned this Jul 1, 2022
@NamJwong NamJwong merged commit ca7697b into main Jul 2, 2022
@NamJwong NamJwong deleted the algorithm/#76 branch July 2, 2022 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

소수 찾기
1 participant