-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.cpp
More file actions
41 lines (36 loc) · 726 Bytes
/
7.cpp
File metadata and controls
41 lines (36 loc) · 726 Bytes
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
/*
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?
*/
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
bool is_prime(unsigned long long);
int main()
{
int number = 1;
vector<unsigned long long> prime;
while(prime.size() < 10002)
{
if (is_prime(number))
prime.push_back(number);
++number;
}
cout << "10001st prime is " << prime.back() << endl;
}
bool is_prime(unsigned long long n)
{
unsigned long long i,sq,count=0;
if(n==1 || n==2)
return true;
if(n%2==0)
return false;
sq=sqrt(n);
for(i=2;i<=sq;i++)
{
if(n%i==0)
return false;
}
return true;
}