Skip to content

Commit d91d52a

Browse files
authored
Create Gap in Primes.cpp
1 parent e9c7916 commit d91d52a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

5-kyu/Gap in Primes.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <math.h>
2+
#include <queue>
3+
4+
using namespace std;
5+
6+
class GapInPrimes
7+
{
8+
public:
9+
static pair <long long, long long> gap(int g, long long m, long long n);
10+
};
11+
12+
bool isPrime (long long n)
13+
{
14+
if (n < 2) return false;
15+
for (int i = 2; i <= trunc(sqrt(n)); i++)
16+
if (n % i == 0) return false;
17+
return true;
18+
}
19+
20+
pair <long long, long long> GapInPrimes::gap(int g, long long m, long long n)
21+
{
22+
pair <long long, long long> res;
23+
queue <long long> q;
24+
for (int i = m; i <= n; i++)
25+
if (isPrime(i))
26+
{
27+
q.push(i);
28+
if (q.size() == 2)
29+
{
30+
if (q.back() - q.front() == g)
31+
{
32+
res = make_pair(q.front(), q.back());
33+
return res;
34+
}
35+
else q.pop();
36+
}
37+
}
38+
res = make_pair(0,0);
39+
return res;
40+
}

0 commit comments

Comments
 (0)