Skip to content

Commit 794d7bb

Browse files
authored
Merge pull request #102 from Shoyeb45/feature/next-prime
Feature: added solution of next prime
2 parents 3fb3f8f + 6816553 commit 794d7bb

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

7_mathematics/next_prime.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
5+
/*
6+
7+
# Problem: Next Prime (https://cses.fi/problemset/task/3396/)
8+
9+
# Prerequisite:
10+
-> Prime Numbers, Divisors, Sieve
11+
12+
# Main Idea:
13+
-> There is not a large gap between prime numbers. The number of prime numbers from 1 to n is given by π(n) - prime counting function.
14+
-> And π(n) = n / log(n), since there are many prime number and their count increases as we go further. So we can start from n + 1
15+
and keep going until we found the prime number(p > n).
16+
-> For checking prime number we can check all numbers from 1 to sqrt(n) or we can use sieve of Erastosthenes to get all the prime numbers from 1 to 1e6
17+
and since n <= 1e12, so we can efficiently check if the number is prime or not just by checking the divisibility with these primes from 1 to 1e6.
18+
19+
- is_prime_1 : This function is implemented using square root solution
20+
- precompute : it computes all the primes from 1 to 1e6 and return the prime vector
21+
- is_prime_2 : Uses sieve to check if the number is prime or not.
22+
23+
Complexties:
24+
If we use square root method -
25+
1. Time Complexity: O(t * sqrt(n) * k)
26+
2. Space Commplexity: O(1)
27+
If we use sieve method:
28+
1. Time Complexity: O(N(log(log(N))) + t * (N / log(N) * k)), where N = 1e6
29+
2. Space Commplexity: O(N / log(N))
30+
31+
And approximately k can be upto 600 for numbers <= 1e6
32+
*/
33+
34+
/// Checks if the given number is prime or not. Uses sqaure root method to check.
35+
/// TC: O(sqrt(n))
36+
/// @return true if number is prime or false
37+
bool is_prime_1(ll number) {
38+
if (number == 2 || number == 3) {
39+
return true;
40+
}
41+
if (number < 2 || number % 2 == 0 || number % 6 % 4 != 1) {
42+
return false;
43+
}
44+
45+
for (ll i = 3; 1LL * i * i <= number; i += 2) {
46+
if (number % i == 0) {
47+
return false;
48+
}
49+
}
50+
return true;
51+
}
52+
53+
/// Function to generate prime numbers.
54+
/// @return All prime numbers from 1 to 1e6
55+
vector<ll> precompute() {
56+
const int N = 1e6 + 1;
57+
vector<ll> primes;
58+
bool is_prime[N];
59+
fill(is_prime, is_prime + N, true);
60+
is_prime[0] = is_prime[1] = false;
61+
62+
for (int i = 2; i < N; i++) {
63+
if (is_prime[i]) {
64+
primes.push_back((ll) i);
65+
for (ll j = 1LL * i * i; j < N; j += i) {
66+
is_prime[j] = false;
67+
}
68+
}
69+
}
70+
return primes;
71+
}
72+
73+
/// Checks if the number is prime or not. Uses generated primes to check the primality.
74+
/// @return true if number is prime or false
75+
bool is_prime_2(ll number, vector<ll>& primes) {
76+
if (number == 2 || number == 3) {
77+
return true;
78+
}
79+
if (number < 2 || number % 2 == 0 || number % 6 % 4 != 1) {
80+
return false;
81+
}
82+
83+
for (ll prime: primes) {
84+
if (prime == number) {
85+
return true;
86+
}
87+
if (number % prime == 0) {
88+
return false;
89+
}
90+
}
91+
return true;
92+
}
93+
94+
/// Function to solve one testcase
95+
void solve() {
96+
ll n;
97+
cin >> n;
98+
99+
ll next_prime = n + 1;
100+
while (!is_prime_1(next_prime)) {
101+
next_prime++;
102+
}
103+
cout << next_prime << "\n";
104+
}
105+
106+
int main() {
107+
ios::sync_with_stdio(false);
108+
cin.tie(nullptr);
109+
110+
int t;
111+
cin >> t;
112+
113+
while (t--) {
114+
solve();
115+
}
116+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ The solutions are organized by problem categories:
7171
- **Exponentiation 2** - Fast exponentiation with modular arithmetic
7272
- **Sum of Divisors** - Mathematical series calculation
7373
- **Creating Strings** - Combinatorics with factorial calculations
74+
- **Next Prime** - Finding next prime
7475

7576
## 🔧 Implementation Details
7677

0 commit comments

Comments
 (0)