Given a number N, how do we find maximum P*Q < N, such that P and Q are prime numbers?
My (brute force) attempt:
- find a list {P, N/P} for all primes P < √N
- find a list of primes Q, such that Q is the largest prime just below N/P in the list above
- Determine the maximum product P*Q from above
While this brute force approach will work, is there a formal (more sensible) solution to this question?
Example: N=27
√N = 5.196
Candidate primes: 2,3,5 --> [{2,13.5},{3,9},{5,5.4}] ->[{2,13},{3,7},{5,5}]
Solution: Max([2*13, 3*7, 5*5]) = 2*13 = 26
Hence, the brute force solution works.
Taking this one step further, we see that Q_max <= N/2 and if indeed we agree that P < Q, then we have Q >= √N.
We can refine our solution set to only those values {P, N\2}, where N\2 >= √N.
I have opted for integer division "\", since we are only interested in integer values, and integer division is indeed much faster than regular division "/"
The problem reduces to:
Example: N=27
√N = 5.196
Candidate P: 2,3 --> [{2,13},{3,9}] -->[{2,13},{3,7}]
(we drop {5,5} since N\P < √N i.e. 5 < 5.196)
Solution set: max([2*13, 3*7]) = 2*13 = 26
It might look trivial, but it just eliminated 1/3 of the possible solution set.
Are there other clever procedures we can add to reduce the set further?