본문 바로가기
프로그래밍/알고리즘 & 자료구조

[프로그래머스] 소수 찾기 with 파이썬

by Play_With 2023. 12. 29.
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/42839

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

nums="011"

# 1. 숫자 만들기 : 조합
from itertools import permutations
num=[i for i in nums]
num_list=[]
for i in range(1, len(nums)+1):
    num_list+=list(permutations(num,i))

# for i in num_list:
#     a=''.join(i)
#     int_num.append(int(a))
# print(int_num)

int_num=[int(''.join(i)) for i in num_list]

# 2. 소수인지 판정 : 나머지
answer=[]
for i in int_num:
    if i <2:
        continue
    chk=True  #chk를 통해 반복문을 다 돌은후에도 나머지가 0이 없다면을 확인할 수
    for j in range(2, int(i**0.5)+1): # 제곱근을 이용하여 나머지 확인 횟수를 반으로 줄일수
        if i % j ==0:
            chk=False
            break
        else :
            continue

    if chk:
        answer.append(i)

print(len(set(answer)))
반응형

댓글