-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindPrime.cpp
68 lines (53 loc) · 1.04 KB
/
FindPrime.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int findPrime(int n);
bool isPrime(int x);
int main()
{
int a = findPrime(3); //5
cout << "The 3rd prime number is: " << a << endl;
int b = findPrime(58); //271
cout << "The 58th prime number is: " << b << endl;
int c = findPrime(10001); //104743
cout << "The 10,001th prime number is: " <<c << endl;
return 0;
}
int findPrime(int n)
{
if (n<=0) return -1;
if (n==1) return 2; //first prime number is 2
vector<int> prime;
for(int i = 2, j = 0; j<n; i++)
{
if (isPrime(i))
{
prime.push_back(i);
j++;
}
}
return prime.back();
}
bool isPrime(int x)
{
if(x<=1){
return false;
}
// 2 and 3 are prime numbers
else if (x==2 || x== 3 ){
return true;
}
// numbers that are divisible by 2 or 3 are not prime
else if (x % 2 == 0 || x % 3 == 0) {
return false;
}
else {
for (int i = 5; i*i <= x; i += 2) {
if (x % i == 0) {
return false;
}
}
return true;
}
}