-
Notifications
You must be signed in to change notification settings - Fork 1
/
prime_2.cpp
52 lines (41 loc) · 1.34 KB
/
prime_2.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
#include <iostream>
using namespace std;
// Function to check if a number is prime using an optimized algorithm
int isPrime(int num) {
if (num <= 1)
return 0; // Numbers less than or equal to 1 are not prime
if (num <= 3)
return 1; // 2 and 3 are prime numbers
if (num % 2 == 0 || num % 3 == 0)
return 0; // Numbers divisible by 2 or 3 are not prime
// Check for prime using 6k +/- 1 rule
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return 0; // If divisible by i or i+2, not prime
}
}
return 1; // If no divisors are found, it's prime
}
int main() {
int n;
string choice;
while (true) {
cout << "\n1: Check Prime or Not\n2: Exit\nEnter your Choice: ";
cin >> choice;
if (choice == "1") {
cout << "\nEnter the Number: ";
cin >> n;
int check = isPrime(n);
if (check == 1)
cout << "\nThe Number " << n << " is Prime Number\n";
else
cout << "\nThe Number " << n << " is Not Prime Number\n";
} else if (choice == "2") {
cout << "\nThanks for Using the Program\n";
break;
} else {
cout << "\nWrong Choice! Try Again\n";
}
}
return 0; // Exit the program
}