-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsPrime.cpp
More file actions
48 lines (44 loc) · 1.15 KB
/
IsPrime.cpp
File metadata and controls
48 lines (44 loc) · 1.15 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
#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;
// Function to check if a number is prime
bool isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Function to find the nearest prime number
int nearestPrime(int n) {
if (isPrime(n))
return n;
int smaller = n - 1;
int larger = n + 1;
while (true) {
if (isPrime(smaller))
return smaller;
if (isPrime(larger))
return larger;
smaller--;
larger++;
}
}
int main() {
int size;
std::cout << "Enter the size of the hash table: ";
std::cin >> size;
if (isPrime(size)) {
std::cout << "The entered size " << size << " is already a prime number." << std::endl;
} else {
int nearest = nearestPrime(size);
std::cout << "The nearest prime to " << size << " is " << nearest << std::endl;
}
return 0;
}