-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPrimeNo.cpp
66 lines (51 loc) · 1.02 KB
/
PrimeNo.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
#include<iostream>
using namespace std;
//function to check whether a number is prime or not
bool Isprime(int n) {
//prime number- a number which is only divisible by itself and 1
//The definition of a prime number is a positive integer that has exactly two positive divisors-which is 1 and the num itself.
//However, 1 only has one positive divisor (1 itself), so it is not prime
if(n <= 0) {
cout<<"Enter a valid integer"<<endl;
return false;
}
for(int i = 2 ; i<=n/2 ; i++) {
//if divisible by 2 or a multiple of 2
if(n%i==0) {
cout<<"Not a prime"<<endl;
return false;
break;
}
}
cout<<"Is a prime"<<endl;
return true;
}
//TIME COMPLEXITY = O(n)
//recursive solution
//int Isprime(int n ,int i=2) {
//
// if(n > i) {
//
// if(n%i!=0) {
//
// return Isprime(n,++i);
//
//
// }
//
// else {
// cout<<"Not a prime"<<endl;
// return 0;
//
// }
// }
//
// cout<<"Is a prime"<<endl;
// return 1;
//
//
//}
int main() {
Isprime(10092828);
return 0;
}