Create A Program That Will Give You Prime Numbers, Which Average Out To A User-Inputted Value.

For instance, if the user enters 6, the program can print 5, 7. The reason being, both 5 and 7 are prime numbers and they average out to 6.

How Can We Code This?

Although this problem may seem difficult, recall that we solved the prime numbers program question together. Now, all we have to do is add the average number functionality. To begin, let’s create a function that will handle this task. Let’s call it “average_prime”, where we will take in one parameter, which will be the user’s value.

Our Current Progress

Note: The reason we have *2 in the range is so that we can access prime numbers above the actual value. Meaning, to get an average of x, you need at least x-1 and x + 1(if x is not prime itself…). (Also, remember that you can change the range in the is_prime function for loop to (2, round(num/2)) as the numbers will start to become multiples after the halfway point (ex: num / 2 and then num / 4 or 6 etc.) to get a faster response) In addition, we can now create a list called result, which will allow us to store the values. Moreover, to actually find these values, we can use two for loops. How will this help us? Well, we can use these loops to search through the ‘primes’ list to find numbers that average out to the user-entered value. Not to mention, we can use an if statement to check whether the numbers do average out to the value. Thus, let’s implement this.

Too Many Outcomes

This code will give us an answer, but the problem is that our code doesn’t actually stop once it finds a potential solution. To make this a little bit more clear, let’s test this function with the value 200. However, before we do that, let’s print the number of pairs found, the result list itself and the final answer:

We have 14 answers!

For those of you that don’t quite understand, we only have 14 answers because of the fact that the pairs switch over after 173, 227. It is just like a parabola where you start to see a reflection after the axis of symmetry. Regardless, why is this happening? Well, we have 2 for loops and we are only calling the break statement inside of the if statement. Alas, we will use a python trick: else, continue, break:

Adding in these statements will actually force the loops to stop, saving time and memory. Not to mention, we will have a nice clean list containing only 2 values. Hence, let’s test it:


As you can see, the for loops stopped and only gave us 1 possible answer! Note: If you would like to access the code, you can use this view link or you can download the .txt file below:

Lastly, to practice the breakdown technique we learned over the course of the prime number questions (ex: turning a bigger problem into a couple of simpler problems), I have found a mini-challenge problem for you: Problem Link

If you weren’t able to solve that problem, here is the solution that I have come up with. Feel free to try and optimize the code: View Link

Lastly, if you enjoyed this post, please both share and leave a nice rating!