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

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

Python

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

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

๐Ÿ“œ๋ฌธ์ œ


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

๋‚ด ํฌ์ŠคํŒ… : [Python] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2581 - ์†Œ์ˆ˜
์ „์— ์†Œ์ˆ˜์— ๋Œ€ํ•ด ๋‹ค๋ฃฌ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ...๋ผ๋Š” ์ˆ˜๋ชจ๋ฅผ ๊ฒช๊ฒŒ๋จ๐Ÿ˜ญ

๐Ÿ‘‰๐Ÿป๋น„๊ต๋ฒ”์œ„๋ฅผ ์ขํ˜€์•ผํ•œ๋‹ค!
ํ•ด๋‹น ์ˆ˜(num)๋ฅผ '2๋ถ€ํ„ฐ num์˜ ์ œ๊ณฑ๊ทผ'๋งŒํผ๋งŒ ๋‚˜๋ˆ ๋ณด๋ฉด์„œ ์†Œ์ˆ˜์ธ์ง€ ํŒ๋ณ„ํ•ด๋ณด์ž
(num**0.5๊นŒ์ง€๋งŒ ๋น„๊ตํ•ด๋„ ์†Œ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€๋Š” ํŒ๋ณ„ ๊ฐ€๋Šฅ๊ธฐ ๋•Œ๋ฌธ์—!)

ํ•˜์ง€๋งŒ, ์ œ๊ณฑ๊ทผ์„ ์ด์šฉํ•ด๋„ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹คโ“
๐Ÿ‘‰๐Ÿป๋ฌธ์ œ์˜ N,M์˜ ๋ฒ”์œ„๊ฐ€ (1 โ‰ค M โ‰ค N โ‰ค 1,000,000)์ด๋‹ค
๋งŒ์•ฝ 2์ค‘for๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๋ฉด โ†’ for๋ฌธ 2๊ฐœ๋กœ ๋‚˜๋ˆ ์ฃผ์ž

for:
  for: 
    print() #2์ค‘for๋ฌธ์„ ๊ฑฐ์ณ์„œ ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜๋ฏ€๋กœ 
            #O(๋ฐฑ๋งŒ*๋ฐฑ๋งŒ) = O(๋ฐฑ๋งŒ^2)์ด ๋˜์–ด๋ฒ„๋ฆผ ใ… ใ… 
    
    
 โ†“ (ํ•ด๊ฒฐ๋ฐฉ๋ฒ• : ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด ๊ฐ„๋‹จํžˆ ํ•ด๊ฒฐ)
 
 
def prime():
  for: #1์ค‘for๋ฌธ์œผ๋กœ ์ผ๋ถ€ ํ•ด๊ฒฐํ•˜๊ณ 
    return
    
for: #ํ•จ์ˆ˜ ๋ฐ–์—์„œ 1์ค‘for๋ฌธ์œผ๋กœ prime()์„ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด๋ฏ€๋กœ
  prime()   #O(๋ฐฑ๋งŒ+๋ฐฑ๋งŒ) = O(๋ฐฑ๋งŒ)์ด ๋œ๋‹ค!!!
  

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

  1. m,n์„ ์ž…๋ ฅ๋ฐ›๊ณ  def sosu(num)์„ ์ด์šฉํ•˜์—ฌ num์ด ์†Œ์ˆ˜๋ฉด true, ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด false๋ฅผ returnํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์ž
  2. m~n๊นŒ์ง€๋„๋Š” ๋ฐ˜๋ณต๋ฌธ i๋ฅผ 'if sosu(i):'ํ•˜์—ฌ ์†Œ์ˆ˜์ผ๋•Œ๋งŒ ์ถœ๋ ฅํ•˜์ž
    โ€ป์กฐ๊ฑด๋ฌธ(if)์€ true์ผ๋•Œ๋งŒ ๋™์ž‘ํ•˜๋ฏ€๋กœ!

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

import sys
m,n = map(int, sys.stdin.readline().split())

def sosu(num):
    if num == 1: return False
    else:
        for i in range(2, int(num**0.5)+1): #์ œ๊ณฑ๊ทผ์œผ๋กœ ๋น„๊ต
            if num % i == 0:
                return False
        return True

for i in range(m, n+1):
    if sosu(i):
        print(i)

โœ๏ธ1. m,n ์ž…๋ ฅ๋ฐ›๊ณ  ์†Œ์ˆ˜ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜ sosu ์ •์˜

import sys
m,n = map(int, sys.stdin.readline().split())

def sosu(num):
    if num == 1: return False #1์ด๋ฉด ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ False
    
    else:
    	#i๋ฅผ 2๋ถ€ํ„ฐ โ€ปnum์˜ ์ œ๊ณฑ๊ทผ+1(์ด์œ ๋Š” ์•„๋ž˜ ์„ค๋ช…)์œผ๋กœ ์ง€์ •
        for i in range(2, int(num**0.5)+1):
        
        	#i๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋ฉด False
            if num % i == 0:
                return False
                
        #์†Œ์ˆ˜์ด๋ฉด True
        return True

ํ’€์ด๋Š” ์•„๋ž˜๋ธ”๋กœ๊ทธ ์ฐธ๊ณ ํ•˜์˜€์Œ!
https://imdona.tistory.com/m/25

โ€ปi๋ฒ”์œ„๋ฅผ num์˜ ์ œ๊ณฑ๊ทผ๊นŒ์ง€ ํ•˜๋Š” ์ด์œ ?

์ „์ฒด์ผ€์ด์Šค๋ฅผ ๊ฒ€์‚ฌํ•˜๋ฉด ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด "์‹œ๊ฐ„์ดˆ๊ณผ"๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค
(์•„๋ฌด๋ž˜๋„ ๋ฌธ์ œ์—์„œ 1,000,000(๋ฐฑ๋งŒ)๊ฒฝ์šฐ์˜ ์ˆ˜์ธ๋ฐ, 
for๋ฌธ์„ ํ†ตํ•˜๋ฉด ๋ฐฑ๋งŒ*๋ฐฑ๋งŒ์ด ๋˜๋ฏ€๋กœ ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™๊ธดํ–ˆ์Œ)

๐Ÿ‘‰๐Ÿป์ œ๊ณฑ๊ทผ๊นŒ์ง€๋งŒ ๊ณ„์‚ฐํ•˜๋”๋ผ๋„ ์†Œ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€๋Š” ํŒ๋ณ„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.



โ€ปint(num**0.5)์ด ์•„๋‹ˆ๋ผ int(num**0.5)+1?

int(num**0.5)์œผ๋กœ ์ง„ํ–‰ํ•  ๊ฒฝ์šฐ, (num**0.5)๊ฐ’์ด n.n ์ฆ‰, ์†Œ์ˆ˜์ผ ๋•Œ ์†Œ์ˆ˜์  ์•„๋žซ์ž๋ฆฌ๋ฅผ ์ ˆ์‚ญํ•ด๋ฒ„๋ฆฌ๋ฏ€๋กœ
ํ•œ๊ฐ€์ง€ ์ผ€์ด์Šค๋ฅผ ์•„์˜ˆ ๋ฐฐ์ œํ•ด๋ฒ„๋ฆฌ๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— int(num**0.5)+1๋กœ ์ง„ํ–‰!
  

โœ๏ธ2. for๋ฌธ์„ ํ†ตํ•ด sosu()ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜์—ฌ ์†Œ์ˆ˜๋งŒ ์ถœ๋ ฅํ•˜๊ธฐ

for i in range(m, n+1):
    if sosu(i):
        print(i)
โ€ป์กฐ๊ฑด๋ฌธ(if)๋Š” true์ผ๋•Œ๋งŒ ๋™์ž‘ํ•˜๋Š”๋ฐ sosuํ•จ์ˆ˜ return๊ฐ’์ด true/false์ด๋ฏ€๋กœ
  ์†Œ์ˆ˜์ผ๋•Œ๋งŒ(treu) print๋ฌธ์„ ํ†ตํ•ด ์ถœ๋ ฅํ•ด์ค€๋‹ค

๐Ÿ“š์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ค๋ฅ˜์™€ ์ •๋ฆฌ

โ€ป์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜

1. ์ œ๊ณฑ๊ทผ์„ ๋‹ค๋ฃจ๊ธฐ ์ „ ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋‚ด ํฌ์ŠคํŒ… : [Python] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2581 - ์†Œ์ˆ˜
์ „์— ํ•œ ๋ฒˆ ๋‹ค๋ค˜์œผ๋ฏ€๋กœ ํŒจ์Šค (๋˜‘๊ฐ™์€ ์‹ค์ˆ˜๋ฅผ ๋ฐ˜๋ณตํ•˜์ง€ ๋ง์ž)

2. ์ œ๊ณฑ๊ทผ์„ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ๋„ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋– ๋ฒ„๋ฆฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜(2์ค‘for๋ฌธ, O(๋ฐฑ๋งŒ^2))

import sys
input = sys.stdin.readline

m,n = map(int,input().split())

for i in range(m,n+1):
    err = 0
    for j in range(2, int(i**0.5)+1):
        if i > 1:
            if i%j == 0:
                err += 1
    if err == 0:
        print(i)
์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋งž์™œํ‹€(๋งž๋Š”๋ฐ ์™œ ํ‹€๋ฆผ?)๋‹จ๊ณ„์—์„œ ํ”ํžˆ ํ•˜๋Š” ์‹ค์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค.
๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋ณ€์ˆ˜์˜ ๋ฒ”์œ„(1 โ‰ค M โ‰ค N โ‰ค 1,000,000)์— ์ฃผ์˜ํ•˜์ž!!
์œ„ ์˜ค๋ฅ˜์ฝ”๋“œ๋Š” O(๋ฐฑ๋งŒ*๋ฐฑ๋งŒ) = O(๋ฐฑ๋งŒ^2)์ด๋ฏ€๋กœ ๋‹น์—ฐํžˆ ์‹œ๊ฐ„์ดˆ๊ณผ ๋  ๊ฒƒ์ด๋‹ค๐Ÿคฆ๐Ÿปโ€โ™€๏ธ

  1. ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋”ฐ๋กœ ์™ธ์›Œ๋‘์ž (์ œ๊ณฑ์€๋งŒํผ๋งŒ ๋‚˜๋ˆ„๊ธฐ!)
  2. ์กฐ๊ฑด๋ฌธ(if)๋Š” True์ผ๋•Œ๋งŒ ๋™์ž‘ํ•œ๋‹ค (ํ•จ์ˆ˜๋‚˜ ๋”•์…”๋„ˆ๋ฆฌ ๊ตฌํ˜„์‹œ ์ฐธ๊ณ )
  3. ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋ฒ”์œ„๋ฅผ ๋จผ์ € ๋ณด๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ค๊ณ„ํ•˜์ž
    '๋ฐฑ๋งŒ*๋ฐฑ๋งŒ'์€ ๋”ฑ๋ด๋„ ์˜ค๋‹ต๊ฐ™์ž–์•„.... ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ๋‚˜ ์ฐพ์•„๋ณด๋Š” ์‹œ์•ผ๋ฅผ ๊ธฐ๋ฅด์ž...
profile
keynene
post-custom-banner

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