My Soltuion for Problem C : Consecutive Primes (Kickstart Round B 2021)

Revision en2, by chinmayajha, 2021-04-19 09:14:26

This being my second Kickstart round ever, and my 4th month into Competitive Programming (without any prior knowledge), I was proud of myself when I got an approach to Problem C, like within 20 seconds after I read it. So here goes my approach (which also happens to be similar to Kickstart's official analysis for the Test Set 3). UPD : I decided to make a YouTube video fir this question (in Hindi) : So for the solution + approach mentioned below please check the video.

The Solution

  • We're given a number N, and we've to find the maximum numnber X less than or equal to N, which should be the product of any two consecutive prime numbers (if that makes sense).
  • The best way would be to find two consecutive primes near the square root of N.
  • Let's call the required primes a1 and a2. a1 and a2 are to be assigned to floor value of sqrt(N) and to a1+1 respectively.
  • This way we get two numbers not necessarily prime near sqrt(N). Now we decrement a1 until it becomes a prime number, and increment a2 until it becomes a prime number. (Note that if any of them are already primes we stop). This step is guaranteed to give us two consecutive primes around sqrt(N).
  • Now that we have a1 and a2 (consecutive primes), we check that is a1*a2 > N. If No, then we output a1*a2 and we're done with the solution.
  • If Yes, then we have 2 options. Either move a1 and a2 to the next prime numbers possibloe, but this would only increase the product, therefore this isn't viable. The other option would be to move a1 and a2 to the preivious prime numbers possible. This is guaranteed to give the required two consecutive primes. (We can simply do a2 = a1; while(!prime(a1))a1--;)
  • And then we just simply output a1*a2 as the result.

Here goes the Code for the above explanation. ~~~~~

include <bits/stdc++.h>

include

using namespace std;

//Function to check if a given number is Prime or not bool test(long long int n){ if (n <= 1) return false; else if (n <= 3) return true; else if (n % 2 == 0 || n % 3 == 0)return false; for (long long int i = 5; i * i <= n; i = i + 6){ if (n % i == 0 || n % (i + 2) == 0)return false; } return true; } int main() { int t;cin >> t; for(int q=1;q<t+1;++q) { long long int n;cin>>n; long long int root = sqrt(n); long long int a1 = root; long long int a2 = a1+1; while(test(a1)!=true){ a1--; } while(test(a2)!=true){ a2++; } while(a1*a2 > n){ a2 = a1; a1--; while(test(a1)!=true)a1--; } cout << "Case #" << q << ": " << a1*a2 << "\n"; } } ~~~~~ Thank You Regards Chinmay Jha chinmayajha

Tags #googlekickstart, # solution, #editorial, #video solutions

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en8 English chinmayajha 2021-04-19 19:59:43 2
en7 English chinmayajha 2021-04-19 10:08:45 4 Tiny change: 'hank You\nRegards \n[user:ch' -> 'hank You\n\nRegards \n\n[user:ch'
en6 English chinmayajha 2021-04-19 09:29:02 33
en5 English chinmayajha 2021-04-19 09:20:39 14 Tiny change: '.net/pile/1n532oBL)\n\n(Trie' -> '.net/pile/pZq8N5VP)\n\n(Trie' (published)
en4 English chinmayajha 2021-04-19 09:17:46 5
en3 English chinmayajha 2021-04-19 09:17:00 844
en2 English chinmayajha 2021-04-19 09:14:26 13
en1 English chinmayajha 2021-04-19 09:04:02 2747 Initial revision (saved to drafts)