How Can We Program This?

Note: For this solution, I will be using python code… To access my work, you can download the .txt file below or access the code through the view link –> use whatever option is easier for you. Then, you can copy and paste it into your compiler. I recommend sublime text or jupyter notebook…. Now, let me walk you through how I was able to create this:

To begin, this problem is really just a set of miniature problems. In particular, we first have to make a prime checker, and then we can apply that function inside of another function where we check every value inside a user-entered range. So, let’s make a prime checker: Recall that a prime number is any integer that can be divided by only 1 and itself, such as 3 or 5. Thus, to check whether a number is prime, all we need to do is run a for loop that uses every value in the range of (2, the number) –> as python ranges go up to but not including…. Next, if the value n is divisible by any one of those numbers, then we can break out of the loop and return false. Otherwise, we will know that the value is prime and we can simply return true:

Prime Checker Function

Now, we can create another function, which would simply iterate through the user-entered range. Moreover, if the value turned out to be prime, it would be appended to our defined prime numbers list. Finally, we can just print out the result:

Prime List Function

Therefore, we were able to simplify this complex problem into something much more manageable. Not to mention, we were able to view this problem from a logical perspective, making it much more easier for us to code. Now that we have understood how to code this, let’s talk about optimization: When we used the for loop to check for every number in the range of 2 to the number itself, aren’t we actually repeating values? Specifically, if we look at the number 10, when you check for 2 and then check for 4 or 6 later, you are performing unnecessary calculations. The reason being, if a number cannot be divisible by 2, it cannot be divisible by 4 or 6. Therefore, we can change this range to (2, round(num/2)), which will make the code run much faster (especially for large numbers). With this learning in mind, do you think you can solve this challenging problem?

Now, Can You Solve This Challenging Problem?

We just worked on breaking down problems into more simple tasks. Now, I want you to put that skill to the test. Here is your problem: In python, how can we switch the value of 2 variables without creating any additional variables?

Solution With A New Variable

The solution above does the job, but does not meet our requirements. Do you think you can complete the task? Are you stuck? Don’t worry. Here is the real solution:

Solution Without Any New Variables!

How is this actually working? Well, when we perform b = a – b, we are actually performing a + b – b so we end up getting a. In addition, when we later perform a = a – b, we are really performing a + b – a (because b’s value switched) so we get b. Thus, that is how we end up switching the values without creating any additional variables because we made a’s value the sum of a and b. For this reason, we were able to take advantage and manipulate the variables. Lastly, as always, if you enjoyed this post, please both share with your friends and leave a nice rating. 😀