Python program to print prime numbers less an inputted number - Codicaly
In this post, we are going to discuss how to write a python program to print the prime numbers less than an inputted number. We already posted a how-to check whether the inputted number is prime or composite. I highly recommend reading that and come here by clicking the link below.
Link to the post : https://codicaly.blogspot.com/2021/04/python-program-to-find-given-number-is.html
What is a Prime or Composite number ?
A prime number is a number that has only two factors i.e, 1, and the number itself. A composite number is a number that has more than two factors. A number is said to factor that when it divides the other number it leaves no remainder. Generally, the factors lie between 1 and the number itself.
Pre-requisites :
- Python Basics
- List Concept
- Conditional statements ( Nested for-loop )
Program :
u=int(input('Enter the upper limit'))
for i in range(2,u+1):#Primary for loop
l=[]
for j in range(1,i+1):#secondary for loop
if (i%j==0):
l.append(j)
if len(l)==2:
print(i)
Note : Please refer to the main pic.
Explanation :
1. First we need to input the number up to which we need to print the prime numbers. We are assigning that to u which is the upper limit. We are specifying it as int otherwise it would take that as a string.
2. Generally, prime numbers start from 2. Our primary goal is to print the prime numbers less than an inputted number. So we are using for loop. In this program, we are going to use a nested for-loop i.e, for loop inside a for loop. This for loop is the primary for loop as we can see it is written with no indentation. The variable used for loop is 'i'. As we know the first argument used in the range is the start value. The second argument is stop value. Generally, the stop value is n-1. So we entered u+1. Now 'i' will vary from 2 to u.
3. This statement is under the primary for loop as we can see the indentation. Here we are creating an empty list to save the factors of the 'i'. It will be emptied after every change in the 'i' value.
4. We need to check whether the 'i' is prime or not. We are calculating the number of factors of 'i'. If it is 2, we know that it is prime. For that, we are using secondary for loop, which is written under the primary loop. We are using 'j' as a variable in the secondary for-loop. The factors of a number always vary from 1 to that number. Hence the start value is 1 and the stop value is i+1. We wrote i+1 in the stop values for the same reason as in the primary loop. Now the 'j' value varies from 1 to 'i'.
5. This if statement is written under the secondary for-loop. We need to check whether the 'j' value is a factor of 'i' or not. We a number is a factor of another number if the division of another number by that number leaves no remainder. For that, we are using the modulus (%) operator. It leaves the remainder when 'i' was divided by 'j'. We are checking that the remainder is zero using the logical operator equal to (==) operator. If the L.H.S is the same as R.H.S, it would return true otherwise false. This is the if condition.
6. This statement is written under the above if statement. If the if condition is true, we know that 'j' is a factor of 'i'. If that is true, we are adding them to the empty list that we created in step 3.
7. This if is written inside the primary for-loop. Now, we need to check whether the number of factors is 2 or not. If it is 2, we know it is prime.
We are using the equality operator ( == ) to check the number of factors.
8. This statement is written under the if statement explained in step 7. If the if condition i.e, number of factors is 2, we know its prime. We need to print that 'i' value. Hence, we are using the print statement and this is only executed if the if condition is true.
- If the number is prime after it is printed, the 'i' value will be changed to the next number and the list 'l' will be emptied and subsequent steps are done until the primary for-loop ends.
- If the number is not composite, it can't enter step 7 as the number of factors will be more than 2.Then the 'i' value will be changed there itself, and list 'l' is emptied and subsequent steps are done until the primary for loop ends.
Thank you
Comments
Post a Comment