Day 10 | Panoramix's Prediction | C++
- Ayush Mahajan
- Aug 28, 2022
- 2 min read
There are multiple good things about this question.
It is easy, so you can easily solve it.
This is a simple question with a lot of distracting background story.
It is about prime numbers, which are used way too much in competitive programming
We can learn some techniques which are overkill for this question but very useful for the future.
Let's start with solving this question first. I would ask you to read the question 2-3 times to try to understand what it is asking because we will gradually need a skill to effectively understand how to read problems with a lot of unnecessary background stories.
The problem is ultimately asking if M is the next prime number after N
To solve this, we can find the next prime number after N and check if it is M. We can run an infinite loop through N+1 to infinity and check if a number is prime or not. If it is prime, we will find our next prime number.
How do we check if a number is prime or not
To answer this question, let's ask ourselves what is a prime number. It is a number that is not divisible by any number other than 1 and itself. So, we can simply follow the definition and check if it actually is divisible by any number between 2 to X-1 [X = Number we are checking for prime]
Like this,
bool is_prime(int X){
for(int i = 2; i < X; i++){
if(X % i == 0) {
/**
* If X is divisble by i, we will return false
* As that states, it is not prime and
* our function - is_prime(X) -> false
***/
return false;
}
}
return true; //Default case, if we could'nt find any number x
//divisible by.
}We can similarly write the complete solution like this - Link to Codeforces Submission
#include<iostream>
using namespace std;
bool is_prime(int x){
for(int i = 2; i < x; i++){
if(x % i == 0)return false;
}
return true;
}
int main(){
int n, m;
cin >> n >> m;
// Finding next prime number
int next = n+1;
while(true){
if(is_prime(next))break;
next++;
}
if(next == m){
cout << "YES";
}else{
cout << "NO";
}
return 0;
}
Time to over-optimize this solution
Here, we were checking primes up to N = 50 only (read constraints in the question). But we might also need to do so for big numbers in the future. So let's bring some technique which is very common.
Sqrt Prime Check Do this activity, take any number of your choice (say X) and write down its factors. Let's say A and B are two factors. and, A > sqrt(X) and also B > sqrt(X) then A x B > X This is not possible as we know A x B = X thus, we can say if A > sqrt(X), then B < sqrt(X) So if we are unable to find any factor of X till sqrt(X), we won't find it after sqrt(X) as well. So, we can change complexity of is_prime(int X) from O(X) to O(sqrt(X)).
bool is_prime(int X){
for(int i = 2; i*i <= X; i++){
if(X % i == 0) {
return false;
}
}
return true;
}![Day 44 | Binary Search Practice [Leetcode] | C++](https://static.wixstatic.com/media/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg)
![Day 43 | Graph and Tree Practice [Leetcode] II | C++](https://static.wixstatic.com/media/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg)
![Day 42 | Graph Practice [LeetCode] | C++](https://static.wixstatic.com/media/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg)
Great way to approach the problem. Amazing work
Great post!