[Python] 7 – 10001st prime

[문제 링크]
http://projecteuler.net/problem=7

[문제 원본]
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

[문제 설명]
2에서 6번째에 해당하는 소수는 13이다. 그렇다면 10001번째 해당하는 소수는 몇인가?

[소스코드]

# problem_7.py
import custom_math
count = input("Input: ");
i = 2
while 1 : 
    if(custom_math.isPrime(i)) :
        count = count -1
    if(count == 0): 
        break
    i = i+1
print("Number is =", i)
# custom_math.py
import math
 
def isPrime(n): 
    result = True
    for i in range(2, int(math.sqrt(n)) + 1):
        if n%i == 0:
            result = False
            break
     
    return result

[결과]
Input: 10001
(‘Number is =’, 104743)

답글 남기기

아래 항목을 채우거나 오른쪽 아이콘 중 하나를 클릭하여 로그 인 하세요:

WordPress.com 로고

WordPress.com의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Twitter 사진

Twitter의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Facebook 사진

Facebook의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

%s에 연결하는 중