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

Revision en4, by chinmayajha, 2021-04-19 09:17:46

This being my second Kickstart round ever, my 4th month into Competitive Programming (without any prior knowledge), and me being grey, 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 : Code

(Tried my best but couldn't attach code directly to the blog, my bad)

Thank You Regards 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)