Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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
Archives
Today
Total
관리 메뉴

NISSO

[프로그래머스 Lv2][완전탐색] 소수찾기 본문

Coding Test

[프로그래머스 Lv2][완전탐색] 소수찾기

oniss 2021. 7. 2. 17:05
from itertools import permutations
def solution(numbers):
    answer = []
    for i in range(len(numbers)):
        for j in permutations(list(numbers), r=i+1):
            n,s = int(''.join(j)),0
            for i in range(int(n**0.5)+1):
                if n%(i+1) == 0:
                    s += 1
            if s==1 and n>1 or n==2:
                answer.append(n)

    return len(list(set(answer)))

itertools를 사용했었지만 permutations는 처음 써봤다.

그리고 <에라토스테네스의 체>를 공부 안 하고 내 방식대로 소수를 찾았더니 2를 탐지를 못해서 그냥 2면 소수라고 리스트에 넣어줬다.

다른 분들 답은 나중에 보고, 코드 설명도 나중에 쓰는 거로..

Comments