Printing perfect numbers in a given interval - Python - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Sunday, 1 November 2020

Printing perfect numbers in a given interval - Python


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


You can find the final code at the end of this article. But I would suggest you to rather stick with us this whole journey as we are going to go through each and every step you need to follow to become the best coder in the world.


Excited? Let's start with the overview!


Overview


In this program, we are asked to print perfect number in a given interval. In simple words, it means that we need to print perfect numbers in a range given by the user. 


Basically, perfect numbers are such numbers whose sum of divisors (excluding itself) is equal to the number itself.


For example: For the number 6:

Divisors (excluding itself) are: 1, 2 and 3. Their sum i.e., 1+2+3 = 6, which is the number itself. Hence 6 is a perfect number. 


You can check out one of our older tutorials where we have explained the full process of finding if a number is a perfect number or not. The only difference in this program is that now we are printing all the perfect numbers belonging to a range. 


Our game plan for this program would be to first ask the user to enter the lower and upper limit. We have to check a number if it is a perfect number or not (process in the tutorial given below). And we have to do that for all the numbers belonging to the range.


Check it out here: Check whether a number is a perfect number or not - Python


*You should check it because we are not going to again discuss the procedure to check a number for perfect number, because then the article would be too long. We are only going to print perfect numbers in a range in this tutorial.*


For example: In the range of 3 to 100,

Perfect numbers are, 6 and 28. 


The only thing different in this tutorial is that rather than checking only one number, we have to now check all numbers belonging to a range for perfect numbers. We would be adding another for loop for this task.


*As I have already told you the basics of the program, you can try to do it on your own. Check whether the answer matches!*


Understanding what we need to do:


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


Step1: Asking the user for the input


The only input we require from the user is of the starting and end points i.e., the range. Hence, we would ask the user to first input the lower limit, then the upper limit. Also, don't forget to typecast them into int as we need to perform calculations on them.


Here's the code for this step:


Step 2: Printing perfect numbers in the given interval


Now comes the main part. As I have already told you, much of our program derives its roots from the previous article on the same topic. Hence, it would be good if you read that as I have explained each line of code of that program.


*Note: Don't copy the whole code from the previous article. There are many minor adjustments we need to make in that code to suite this program. Just refer it to understand what are we actually doing.*


Now what we need to do with the above code (from the previous tutorial) is that, we just need the for loop part, which will help us to check if a number is a perfect number or not. Only this part is necessary from the previous program. I only recommended it so that you can understand the basics on what our program is based.


The code we just copied divides a specific number with all the number ranging from 1 to the (number-1). If any number divides it, then the if statement runs and it adds the value of that number to another variable sum, which stores the sum of all these divisors.


After this loop ends, we will check if the final sum is equal to the number. If it is, then we will print the number. And this process should happen for each number belonging to the range (given by user). So, we would use another for loop for this purpose. 


Also, in the previous tutorial on perfect numbers, we declared the sum variable before the subordinate for loop (that checks if the number is a perfect number). But this time, as we have to check condition for many numbers, we want value of sum to become 0 after each number is checked. So we won't declare the sum variable inside the subordinate for loop but inside the main for loop.


So, the chronology of the program is:


  1.  The user enters the upper and the lower limits.
  2.  For loop runs for the first number belonging to the range.
  3.  Initially value of sum is 0.
  4.  For loop again runs to divide the number from each number from 1 to number-1.
  5.  The If statement runs to check if the remainder is zero (divisibility check). 
  6.  If it is, then the value of that divisor is added to the variable sum. And this loop ends after dividing the main number with all numbers other than the number itself. 
  7.  After this loop ends, then we check if the final sum is equal to the main number or not.
  8.  If it is, then the value of the main number is printed.
  9.  And if it isn't then the main for loop does all this again for the all the numbers in the given range.
  10.  After this for loop ends, we get our final output! Done!

I hope that you understood the code. If you didn't then please check the tutorial whose link was given 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 to the code. That is the best way to learn how to code. Learning from our own failure, that what coding is! Then also, comment section is always open for your queries.

Here's our code for this step:

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

Here's the final code:

Here's the output of this code:

Printing perfect numbers in a given interval - Python


This is the end of our amazing journey where we found the best way to print perfect numbers in python. I hope you learnt 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