๐ŸŒฑ → ๐ŸŒณ

[๋ฐฑ์ค€] 4948๋ฒˆ: ๋ฒ ๋ฅดํŠธ๋ž‘ ๊ณต์ค€ ๋ณธ๋ฌธ

Algorithms

[๋ฐฑ์ค€] 4948๋ฒˆ: ๋ฒ ๋ฅดํŠธ๋ž‘ ๊ณต์ค€

BAY 2022. 7. 30. 14:23
728x90

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

 

4948๋ฒˆ: ๋ฒ ๋ฅดํŠธ๋ž‘ ๊ณต์ค€

๋ฒ ๋ฅดํŠธ๋ž‘ ๊ณต์ค€์€ ์ž„์˜์˜ ์ž์—ฐ์ˆ˜ n์— ๋Œ€ํ•˜์—ฌ, n๋ณด๋‹ค ํฌ๊ณ , 2n๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์†Œ์ˆ˜๋Š” ์ ์–ด๋„ ํ•˜๋‚˜ ์กด์žฌํ•œ๋‹ค๋Š” ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋‹ค. ์ด ๋ช…์ œ๋Š” ์กฐ์ œํ”„ ๋ฒ ๋ฅดํŠธ๋ž‘์ด 1845๋…„์— ์ถ”์ธกํ–ˆ๊ณ , ํŒŒํ”„๋ˆ„ํ‹ฐ ์ฒด๋น„์‡ผ

www.acmicpc.net

 

์ฒ˜์Œ ์ฝ”๋“œ:

while(1):
  a=int(input())
  if a ==0:
    break
  cnt=0
  for i in range(a+1,2*a+1):
    if i == 1:
      continue
    for j in range(2,int(i**0.5)+1):
      if i % j == 0:
        break
    else:
      cnt+=1
  print(cnt)
๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ๊ณ„์† ์†Œ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋‹ˆ๊นŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒ
 
 
์ •๋‹ต ์ฝ”๋“œ:
sosu =[]
for i in range(2,246913):
  cnt = 0 
  for j in range(2, int(i**0.5)+1):
    if i % j ==0:
      cnt += 1
      break
  if cnt == 0:
    sosu.append(i)
    
while(1):
  a = int(input())
  count = 0
  if a == 0:
    break
  for i in sosu:
    if a < i <= 2*a:
      count +=1
  print(count)

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋ฒ”์œ„ ๋‚ด์—์„œ ์†Œ์ˆ˜๋ฅผ ๋จผ์ € ๋ชจ๋‘ ๊ตฌํ•˜๊ณ  ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ ํ•ด๊ฒฐ

728x90