[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ5] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2581 - ์†Œ์ˆ˜

keyneneยท2022๋…„ 10์›” 18์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
10/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ5] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2581 - ์†Œ์ˆ˜

๐Ÿ“œ๋ฌธ์ œ



๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

์ž…๋ ฅ๊ฐ’m,n๊ณผ number(m~n๊นŒ์ง€)๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•˜์ž
sosu๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์†Œ์ˆ˜๋ฅผ ์ €์žฅํ• ๊ป€๋ฐ,
number[x]๋ฅผ ์ˆœ์„œ๋Œ€๋กœ '2~(number[x]-1)'๋กœ ๋‚˜๋ˆ ๋ณด๋ฉด์„œ ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด
(number[x]๋ฅผ '2~number[x]-1'๊นŒ์ง€ ๋‚˜๋ˆด์„ ๋•Œ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด)
์†Œ์ˆ˜์ด๋ฏ€๋กœ sosu์— ์ €์žฅํ•˜๊ณ 
๋‚˜๋จธ์ง€๊ฐ€ 0์ธ ์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋ฉด ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐ€๊ฒŒ ๊ตฌํ˜„ํ•˜์ž


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. m, n, number์ €์žฅ, sosu ๋นˆ๋ฆฌ์ŠคํŠธ ์„ ์–ธํ•˜๊ธฐ
  2. number[x]๊ฐ€ '2~number[x]-1'๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋ฉด ์†Œ์ˆ˜X, ์•„๋‹ˆ๋ฉด sosu์— ์ €์žฅํ•˜๊ณ 
    ์†Œ์ˆ˜ํ•ฉ sum(sosu)๊ณผ ์ตœ์†Œ๊ฐ’sosu[0]์„ ์ถœ๋ ฅํ•˜์ž

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys
input = sys.stdin.readline

m = int(input().rstrip())
n = int(input().rstrip())
number = [i for i in range(m,n+1)] #m~n๊นŒ์ง€ ์ €์žฅ(๊ฒ€์ƒ‰์šฉ)
sosu = [] #์ €์žฅ์šฉ

for num in number:
    err = 0
    if num > 1: #1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ˆ๊นŒ ํŒจ์Šค
    
    	#์†Œ์ˆ˜๊ฐ€ ์•„๋‹Œ๊ฒฝ์šฐ err += 1
        for i in range(2,num):
            if num % i == 0:
                err += 1
                
        #์†Œ์ˆ˜์ธ๊ฒฝ์šฐ sosu์— ๊ฐ’ ์ €์žฅ
        if err == 0:
            sosu.append(num)
if len(sosu) == 0:
    print(-1)
else:
    print(sum(sosu),sosu[0], sep="\n")

โœ๏ธ1. ์ž…๋ ฅ๊ฐ’์ €์žฅ, number(๊ฒ€์‚ฌ๋ฆฌ์ŠคํŠธ) ๋ฐ sosu(์ €์žฅ๋ฆฌ์ŠคํŠธ) ์„ ์–ธ

import sys
input = sys.stdin.readline

m = int(input().rstrip())
n = int(input().rstrip())
number = [i for i in range(m,n+1)]
sosu = []

โ€ปm~n๊ฐ’ number์— ์ €์žฅํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

number = []
for i in range(m,n+1):
  number.append(i)

ํ˜„์˜ˆ์ œ์—์„œ๋Š” ๊ตณ์ด .append()๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  
๋ณธ์ฝ”๋“œ๋Œ€๋กœ ํ•œ์ค„์— ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ํšจ๊ณผ์ ์ด๋‚˜,
์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ์ถ”๊ฐ€ ๊ณ„์‚ฐ์„ ํ•˜๊ณ  ์ €์žฅํ•  ๋•Œ๋Š” ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์ด ํšจ๊ณผ์ ์ผ ์ˆ˜ ์žˆ์Œ

โœ๏ธ2. ์†Œ์ˆ˜์ธ ๊ฐ’ / ์†Œ์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฐ’ ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅ ๋ฐ ์ถœ๋ ฅ

for num in number:
	#์†Œ์ˆ˜๋ฅผ ํŒ๋ณ„ํ•˜๋Š” err๊ฐ’ ์ดˆ๊ธฐํ™”
    err = 0
    
    #1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ 1์ด์ƒ์˜ ์ˆ˜๋งŒ ํ™•์ธ
    if num > 1:
        for i in range(2,num): #2~num-1๊นŒ์ง€ i๋ฅผ
            if num % i == 0:   #num์— ๋‚˜๋ˆ ์„œ ๋‚˜๋จธ์ง€ ๊ฐ’์ด 0์ด๋ฉด
                err += 1       #err += 1ํ•˜์—ฌ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹˜์„ ํŒ๋ณ„!
        if err == 0:           #์™œ๋ƒ๋ฉด err == 0(๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ๊ฐ’์ด ์—†๋Š” ์ˆ˜๋งŒ)
            sosu.append(num)   #sosu๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ• ๊บผ๋‹ˆ๊นŒ!
            
if len(sosu) == 0: #sosu์— ์ €์žฅ๋œ ๊ฐ’์ด ์—†์„ ๋•Œ (์ดˆ๊ธฐ๊ฐ’๊ณผ ๊ฐ™์ด ๋นˆ๋ฆฌ์ŠคํŠธ์ผ ๋•Œ)
    print(-1)
else: #๋นˆ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹ˆ๋ฉด ํ•ฉ๊ณ„, ์ตœ์†Œ๊ฐ’ ์ถœ๋ ฅ
    print(sum(sosu),sosu[0], sep="\n")

๐Ÿ“š์ •๋ฆฌ

๊ฐœ๋…์€ ์•Œ๊ณ ์žˆ์œผ๋ฏ€๋กœ ํฌ๊ฒŒ ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์•„๋‹ˆ์—ˆ๋Š”๋ฐ ์†Œ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š”์ง€๊ฐ€ ๊ด€๊ฑด์ด์—ˆ์Œ
์†Œ์ˆ˜๋Š” 2์ด์ƒ์ธ ์ˆ˜์— ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š” ์ˆ˜์ด๋ฏ€๋กœ,
๋ง ๊ทธ๋Œ€๋กœ '2~์ž๊ธฐ์ž์‹ -1'๊นŒ์ง€ ๋‚˜๋ˆ„๋ฉด์„œ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค ์™„์„ฑํ•˜์˜€์Œ
์ด๋ณด๋‹ค ๋œ ๋ฌด์‹ํ•˜๊ณ  ๋น ๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ,
๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋– ์˜ฌ๋ž๊ณ  ๋นจ๋ฆฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ์œผ๋ฏ€๋กœ ํŒจ์Šค....

profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€