-
[준비운동 PART1. 튼튼한 기본기] 소수(2581번)🧠𝗔𝗹𝗴𝗼𝗿𝗶𝘁𝗵𝗺/💛 백준 2021. 7. 2. 11:04
1. 문제 설명
https://www.acmicpc.net/problem/2581
2. 나의 풀이
(1) 테스트케이스는 다 맞았으나 틀렸다고 나온 코드
m = int(input()) n = int(input()) def primeList(x): sieve = [True] * x y = int(x ** 0.5) for i in range(2, y+1): if sieve[i] == True: for j in range(i+i, x, i): sieve[j] = False return [i for i in range(2, x) if sieve[i] == True] m_list = set(primeList(m)) n_list = set(primeList(n)) answer_list = list(set(n_list) - set(m_list)) if len(answer_list) == 0: print(-1) else: print(sum(answer_list)) print(min(answer_list))
위 경우에, m이상 n이하라고 했으므로, n까지의 소수에서 m까지의 소수를 빼주면 될 것 같아서 이렇게 구현했었다.
그런데 틀렸다고 나와서 왜 틀리는지 알 수 없었다 .. ㅠㅠ 😱
(2) 최종 정답
m = int(input()) n = int(input()) def primeList(x): sieve = [True] * x y = int(x ** 0.5) for i in range(2, y+1): if sieve[i] == True: for j in range(i+i, x, i): sieve[j] = False return [i for i in range(2, x) if sieve[i] == True] answer_list = [num for num in primeList(n+1) if m <= num <= n] if len(answer_list) == 0: print(-1) else: print(sum(answer_list)) print(min(answer_list))
그래서 answer_list 구하는 부분을 저렇게 수정했더니 ,, 통과했다 ....!
'🧠𝗔𝗹𝗴𝗼𝗿𝗶𝘁𝗵𝗺 > 💛 백준' 카테고리의 다른 글
[준비운동 PART1. 튼튼한 기본기] 쉽게 푸는 문제 (1292번) (0) 2021.07.02 [준비운동 PART1. 튼튼한 기본기] 소수 찾기(1978번) (0) 2021.07.02 [준비운동 PART1. 튼튼한 기본기] 최대공약수와 최소공배수(2609번) (0) 2021.07.01 [준비운동 PART1. 튼튼한 기본기] 일곱난쟁이(2309번) (0) 2021.07.01 [준비운동 PART1. 튼튼한 기본기] 지능형 기차 2(2460번) (0) 2021.07.01