ProjectEuler 007题

题目: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?

方法一:

 1 #include<iostream>
 2 using namespace std;
 3 bool isPrime(long long num);
 4 int main() {
 5     int count = 1;//考虑到2为第一个素数
 6     long long num = 3;
 7     while(true) {
 8     if(isPrime(num))
 9         count++;
10     if(10001 == count){
11         break;
12     }
13     num++;
14     }
15     cout << num;
16     system("pause");
17     return 0;
18 }
19 bool isPrime(long long num){
20     int i=0;
21     for(i = 2; i*i <= num; i++){
22 
23         if(num%i == 0)
24             return false;
25     }
26     return true;
27 }

方法二:

 1 #include<iostream>
 2 using namespace std;
 3 typedef unsigned int uint;
 4 const uint N = 200000;
 5 int main() {
 6     uint *n = new uint[N];
 7     uint count = 0;
 8     for(uint i =0; i<N; i++) {
 9         n[i]=i;
10     }
11     for(uint j = 2; j < N;j++) {
12         if(n[j] != 0){
13             count++;
14             cout << n[j] << endl;
15             if(10001 == count){
16                 cout << n[j] << endl;
17                 break;
18             }
19             for(uint k = j+1; k < N;k++){
20                 if(n[k]!=0 && n[k] % j == 0)
21                     n[k]=0;
22             }
23         }
24     }
25     delete [] n;
26         cout << num;
27     system("pause");
28     return 0;
29 }

 

posted @ 2014-05-25 20:47  soul390  阅读(179)  评论(0编辑  收藏  举报