Editorial for CCC '19 S2 - Pretty Average Primes
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
This problem asks us to find two primes,  and 
, that satisfy 
. Equivalently, 
.
Approach 1
A straightforward approach is to test all possible values of  and 
. For example, we can use a for-loop that goes from 
 to 
. Then we can calculate 
 using the equation 
. Once we have 
 and 
, we need to check if 
 and 
 are prime by using a primality test.
One approach is trial division. Suppose  is the input number. If the for-loop iterates from 
 to 
, then trial division will take 
 time.
In practice, the overall time complexity is slightly slower than . This is enough to get 6/15.
A slightly faster approach is to use trial division, ending at  (or 
floor(sqrt(x))). Trial division will take  time.
In practice, the overall time complexity is slightly slower than . This is enough to get 15/15.
Approach 2
Since  and 
 are at most 
, we can precompute all the primes up to 
 using the sieve of Eratosthenes. This will take 
 time. Then we can use Approach 1 with each primality test taking 
 time.
The worst possible test case is  which requires 
. As a result, the overall time complexity is 
. This is enough to get 15/15.
Note: These 2 approaches have an time complexity. For context, please see this comment. Please don't ask for a proof of the time complexity.
Comments