Algorithm, Data structure/Solved Algorithmic Problem

Project Euler #7 - 10001st prime

JaykayChoi 2016. 6. 11. 14:00

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?


출처: https://projecteuler.net/



10,001번째의 소수를 구하는 문제입니다. 

앞전 문제와 마찬가지로 Eratosthenes' sieve 을 이용해 소수들을 구하는 방법으로 풀었습니다.




my solving

c++

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
 
using namespace std;
 
 
bool isPrime[10000000];
int maxNum = 1000000;
 
void eratosthenes() 
{
    memset(isPrime, 1sizeof(isPrime));
 
    isPrime[0= isPrime[1= false;
 
    int sqrtn = int(sqrt(maxNum));
 
    for (int i = 2; i <= sqrtn; i++)
    {
        if (isPrime[i])
        {
            for (int j = i*i; j <= maxNum; j += i)
                isPrime[j] = false;
        }
    }        
}
 
 
int main() 
{
    int limit = 10001;
    int ret = 0;
    eratosthenes();
 
    for (int i = 2; ; i++)
    {
        if (isPrime[i])
        {
            limit--;
            if (limit == 0)
            {
                ret = i;
                break;
            }
        }
    }
 
    cout << ret << endl;
 
    system("pause");
    return 0;
}
cs