Printing prime numbers in a given interval (Python) - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Monday, 26 October 2020

Printing prime numbers in a given interval (Python)


Hey all, in this amazing tutorial, we are going to print prime numbers in a given interval. So be ready with your code editors as we are going to do this program in the best way possible!


As always, you can find the final code of this problem at the end of this article, but I would recommend you to be with us this whole journey as we are going to go through each and every step you need to take to become the best coder in the world.


Excited? Let's start with the Overview!


Overview


In this problem, we are asked to write a program in python to print prime numbers in a given interval. In simple words, it means that we have to print prime numbers belonging to a range given by the user. 


For example: In the range of 5 to 14,

prime numbers are 5, 7, 11, and 13.


Before starting off with the explanation, it is necessary for you to know what is our gameplan. So, first, we would be needing the values of the lower and upper limits, which we are going to take from the user. 


This is quite similar to our old program where we find if the number entered by the user is a prime number or not. The only thing different is that now we have to run another for loop on top of the earlier code to do that for all the numbers belonging to the series.


This may help: WAP (Python) to find whether the number entered by the user is a prime number or not


*As I have already told you the basics of the program, it would be good if you try to do this on your own. Check if the solution matches!*


Understanding what we need to do


Here I will tell you step-by-step what you need to do.


Step 1: Taking input from the user


This is the easiest step as we just need to take input from the user. We need to take two inputs from the lower and the upper limits. So we keep the variables name as lower and upper for both respectively. Also, don't forget to typecase their values to an integer using the int command as we need to do calculations on them.


Here's the code for this step:


Step 2: Printing prime numbers in the interval 


Now comes the important part where we will write the main code for this program. As I already said that the only new thing we have to do in this program is to use a for loop above the code we wrote for the other program. You can check that tutorial too for a full step-by-step explanation.


*Note: Do not copy the full code, we will have to make certain changes to it. It is recommended so that you can learn what the code actually does. The only relevant part is the for loop part. In that too, please ignore the print statements as we are not going to use them here.*


So what we need to do is that above that code (from another article), we will just write a for loop which will iterate the value of num (another variable that stores the value of numbers) from (lower, upper+1). *Why upper+1? This is because the range function does not include the end limit. But we also want to check the last number hence, we need to add 1 to it.*


After it, we would add an if statement just to check if the number is greater than 1 or not. As you may know, prime numbers start after 1. In this, if statement, we would put the code we did previously (in another article) which checks if a number is a prime number or not. 


Just copy the for loop part from it. As of now, we don't need to tell the user if it is a prime number or not, we can remove the print statement beneath the if statement. Also, we have to replace the print statement of the else part as we now need to print just the number. 


Hence, in the print statement of the else part, we just need to print(num). 


So the chronology of the program is:


  1. The user enters the upper and lower limits
  2. For loop runs for the first number in the range
  3. If statement checks if the number is greater than 1
  4. For loops runs to divide that number with another number starting from 2 to num-1.
  5. If statement runs. If the number is divisible by any other number, then it isn't a prime number. The lower for loop break and the main for loop runs again with an iterated value of num.
  6. If the number isn't divisible by any number, then we come out of the subordinate loop and the else statement runs. It prints the number to the user.
  7. After this, the loop again runs till it tests all the numbers in the range.
  8. Program Finished! The user gets all the prime numbers belonging to a particular range. Cheers!!!

I hope that you understood the code, if you didn't then please check the article (whose link I gave you earlier), as there I have explained step-by-step all the lines of code. I hope that will definitely clear all your doubts.

But still, if you didn't understand any line of code, just try to test out the program by doing your own modifications. That is the best way to learn to code. Learning from our own failure, that's what coding is! Then also, the comment section is always open for discussion!

Here's the code for this step:

Hence, we have successfully found the best way to print prime numbers in a given interval in python!

Here's the final code:


Here is the output of this code:

Printing prime numbers in a given interval (Python)

This is the end of our amazing journey where we did some brainstorming to find the best solution to this problem. I hope you learned something new today. 

If you did, then don't forget to share it with others as it may help someone struggling with coding!

Also, do subscribe to our newsletter as we do these coding sessions regularly!

Again, thanks for reading!

No comments:

Post a Comment