-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeChecker.cpp
More file actions
104 lines (80 loc) · 1.42 KB
/
PrimeChecker.cpp
File metadata and controls
104 lines (80 loc) · 1.42 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// PrimeChecker.cpp
// ds_hw1_warm_up
//
#include "PrimeChecker.h"
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
using std::vector;
int check_prime(unsigned long num);
unsigned long PRIMECHECKER::PrimeChecker(unsigned long a, unsigned long b){
vector<unsigned long> prime_list;
if (a==1){
if (b == 1){
return 0;
}
else{
a++;
}
}
if ((a % 2) == 0){
if (a == 2){
prime_list.push_back(2);
a++;
}
else{
a++;
}
}
for(unsigned long count = a; count <= b; count = count + 2){
if (prime_list.empty()){
if (check_prime(count)){
prime_list.push_back(count);
}
else{
continue;
}
}
else{
int flag1 = 1;
for(int j = 0; j < prime_list.size(); j++){
if ((count % prime_list[j]) == 0){
flag1 = 0;
break;
}
}
if (flag1 == 0){
continue;
}
else{
if (check_prime(count)){
prime_list.push_back(count);
}
else{
continue;
}
}
}
}
return prime_list.size();
}
int check_prime(unsigned long num){
int flag = 1;
unsigned long i = 3;
while(i * i < num){
if ((num%i) == 0){
flag = 0;
break;
}
i=i+2;
}
/*for(unsigned long i = 3; i <= sqrt(num); i=i+2){
if ((num%i) == 0){
flag = 0;
break;
}
}*/
return flag;
}