View
반응형
문제
- 프로그래머스 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
반응형
'알고리즘 > Python' 카테고리의 다른 글
[프로그래머스] 영어 끝말잇기 - python (0) | 2020.07.26 |
---|---|
[프로그래머스] 짝지어 제거하기 - python (0) | 2020.07.26 |
[프로그래머스] 예상대진표 - python (0) | 2020.07.26 |
[프로그래머스] 점프와 순간이동 - python (0) | 2020.07.26 |
[프로그래머스] 기지국 설치 - python (0) | 2020.07.26 |