코딩테스트 소수 만들기 문제풀이

프로그래머스 소수 만들기(level 1) 문제풀이

from itertools import combinations
import math

def solution(nums):
    n= 2997
    cnt = 0
    array = [ True for _ in range(n+1)]
    for i in range(2, int(math.sqrt(n)) + 1):
        if array[i] == True:
            j = 2
            while i * j <= n:
                array[i*j] = False
                j += 1
    data = [ sum(i) for i in combinations(nums, 3)]
    for d in data:
        if array[d] == True:
            cnt += 1
    return cnt

combinations로 조합을 사용했다 그리고 소수 판별은 에라토네스의 체를 사용했다.

좋은 웹페이지 즐겨찾기