알고리즘/Python
[프로그래머스] 소수만들기 - python
짱호
2020. 7. 26. 16:12
반응형
문제
- 프로그래머스 2018 서머코딩- 소수만들기
- https://programmers.co.kr/learn/courses/30/lessons/12977
풀이
조합 리스트(temp)를 만들고, 소수를 저장하는 리스트(primes)를 만들어
temp의 합을 primes와 비교하는 방법으로 풀었다.
combinations를 이용해 조합을 만들었고, 에라토스테네스의 체를 이용해 소수를 구했다.
코드
from itertools import combinations
def solution(nums):
ans=0
temp = list(combinations(nums,3))
n=10000
a = [False,False] + [True]*(n-1)
primes=[]
for i in range(2,n+1):
if a[i]:
primes.append(i)
for j in range(2*i, n+1, i):
a[j] = False
for i in temp:
if sum(i) in primes:
ans +=1
return ans
반응형