Press "Enter" to skip to content

How to Find Two Primes Whose Sum is Equal to a Number N

Ajk 0

How to Find Two Primes Whose Sum is Equal to a Number N? I have been asked questions about primes in many, many interviews and this is definitely one of the questions that have stuck with me.

First thing first, as always, let us clear out any assumptions. As it turns out, usually problems that involve Maths, are more straight-forward in what they ask. The few questions that you might ask are:

  • What should we do if we find multiple such tuples of two primes whose sum is equal to a number N? For now we can assume that just returning the one which has the smallest number, will be fine.
  • Where do we start counting? Do we count 0 or 1as a possible “prime” number? As it turns out the specific definition of a Prime Number, specifies that a number must be greater than 1 to be considered a prime

    A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Let us try to devise an algorithm, that would somehow solve our problem. A general brute force approach would be to go through all possible numbers and check if they can form a prime tuple whose sum is equal to some number N we are given.

How to Find Two Primes Whose Sum is Equal to a Number N – Algorithm Basics

  • For each number i from 2 to N.
  • If i and N-i are both primes, return the tuple.

Looks pretty simple does it not? The only missing piece is how do we check that the numbers are prime.

How to Find Two Primes Whose Sum is Equal to a Number N – Primality Test for i

  • For each number j from 2 to to i-1.
  • If the remainder of i divided by any of the js is 0, then this is not a prime.

Everything should be pretty clear by now. All we have to do is try to put our solution to code.

How to Find Two Primes Whose Sum is Equal to a Number N – Brute Force Solution

Looks good and simple. Now let’s do a Time and Space complexity analysis. We are only keeping some static sized array as our solution. Hence Space Complexity is O(1). For time on the other hand, we are looking through all items from 1-N, and doing a primality test for all of them, by again going from 1-N. It seems obvious the time complexity is O(N2). NOTE: Even with our nice small optimization of putting a square root on the primality test, the time complexity remains the same. DO NOT mistake square root, with the most commonly utilized in complexity analysis, log(N).

With all that said and done, can we do better? As it turns out we should be able to, but I will leave that to up to you guys this time! Here are a few hints and approaches that we could take:

How to Find Two Primes Whose Sum is Equal to a Number N – Further optimizations

  1. What if instead of checking for numbers being primes from scratch all the time, we reuse some of the work we do?
  2. What if instead of checking for numbers being primes, we compile a huge list of prime numbers up to N and then simply check over those?
The following two tabs change content below.
If you like one of my posts the best way to support is give it a thumbs up, comment, or share it on social media 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *