Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

자르비 왕국

[백준] 2581 소수 - Python 본문

문제풀이

[백준] 2581 소수 - Python

자르비옹스 2022. 1. 18. 22:40

https://www.acmicpc.net/problem/2581

 

2581번: 소수

M이상 N이하의 자연수 중 소수인 것을 모두 찾아 첫째 줄에 그 합을, 둘째 줄에 그 중 최솟값을 출력한다.  단, M이상 N이하의 자연수 중 소수가 없을 경우는 첫째 줄에 -1을 출력한다.

www.acmicpc.net

M = int(input())
N = int(input())

arr = []

for num in range(M, N+1) :
    if num == 1 :
        continue
    idx = 2
    status = True
    while idx < num :
        if num % idx == 0 :
            status = False
            break
        idx += 1
    if status :
        arr.append(num)
        
if not arr :
    print(-1)
else :
    print(sum(arr))
    print(arr[0])