In the previous article, we have discussed Python Program to Add Two Numbers Without Using the “+” Operator.
If the sum of a number’s appropriate divisors (excluding the number itself) equals the number, the number is said to be the perfect number.
Consider the following example: appropriate divisors of 6 are 1, 2, 3. Because the sum of these divisors equals 6 (1+2+3=6), 6 is considered a perfect number. When we consider another number, such as 12, the proper divisors of 12 are 1, 2, 3, 4, and 6. Now, because the sum of these divisors does not equal 12, 12 is not a perfect number.
Python programming is simpler and more enjoyable than programming in other languages due to its simplified syntax and superior readability. Now that we understand the concept of a perfect number, let’s construct a Python program to determine whether or not a number is a perfect number. Let’s write some Python code to see if the given user input is a perfect number or not, and have some fun with Python coding.
- Python Program to Count Divisors of Factorial
- Python Program to Find Common Divisors of Two Numbers
- Python Program to Find Smallest Prime Divisor of a Number
Examples:
Example1:
Input:
Given lower limit range = 1 Given upper limit range=1000
Output:
The Perfect numbers in the given range 1 and 1000 are: 1 6 28 496
Example 2:
Input:
Given lower limit range = 496 Given upper limit range=8128
Output:
The Perfect numbers in the given range 125 and 8592 are: 496 8128
Program to Generate Perfect Numbers in an Interval
Below are the ways to generate Perfect Numbers in a given interval.
Method #1: Using For Loop (Static Input)
Approach:
- Give the lower limit range as static input and store it in a variable.
- Give the upper limit range as static input and store it in another variable.
- Loop from lower limit range to upper limit range using For loop.
- Inside the for loop take a variable to say ‘itrnumb‘ and initialize its value to the iterator value.
- pass the itrnumb as a argument to checkPerfectNumbr() function.
- Inside the checkPerfectNumbr() function Take a variable to say totalSum and initialize it to 1.
- Iterator from 2 to number -1 using for loop.
- Check if the iterator value divides the number using the If conditional statement.
- If it is true then add the given iterator value to totalSum.
-
Check if the totalSum is equal to the given number using if conditional statement.
- If it is true then it is a perfect number then return True.
- Else return False.
- Inside the main function for the loop check whether the function turns return or False.
- If it returns true then print the itrnumb.
- The Exit of the Program.
Below is the implementation:
# function which returns true if the given number is # perfect number else it will return False def checkPerfectNumbr(givenNumb): # Taking a variable totalSum and initializing it with 1 totalSum = 1 # Iterating from 2 to n-1 for i in range(2, givenNumb): # if the iterator value is divides the number then add the given number to totalSum if givenNumb % i == 0: totalSum += i # if the totalSum is equal to the given number # then it is perfect number else it is not perfect number if(totalSum == givenNumb): # if it is true then it is perfect number then return true return True # if nothing is returned then it is not a perfect number so return False return False # Give the lower limit range as static input and store it in a variable. lowlimrange = 1 # Give the upper limit range as static input and store it in another variable. upplimrange = 1000 print('The Perfect numbers in the given range', lowlimrange, 'and', upplimrange, 'are:') # Loop from lower limit range to upper limit range using For loop. for itrvalue in range(lowlimrange, upplimrange+1): # Inside the for loop pass the iterator value to checkNeonnumb() function. if(checkPerfectNumbr(itrvalue)): # If it returns true then print the iterator value. print(itrvalue, end=' ')
Output:
The Perfect numbers in the given range 1 and 1000 are: 1 6 28 496
Method #2: Using For Loop (User Input)
Approach:
- Give the lower limit range as user input and store it in a variable.
- Give the upper limit range as user input and store it in another variable.
- Loop from lower limit range to upper limit range using For loop.
- Inside the for loop take a variable to say ‘itrnumb‘ and initialize its value to the iterator value.
- pass the itrnumb as a argument to checkPerfectNumbr() function.
- Inside the checkPerfectNumbr() function Take a variable to say totalSum and initialize it to 1.
- Iterator from 2 to number -1 using for loop.
- Check if the iterator value divides the number using the If conditional statement.
- If it is true then add the given iterator value to totalSum.
-
Check if the totalSum is equal to the given number using if conditional statement.
- If it is true then it is a perfect number then return True.
- Else return False.
- Inside the main function for the loop check whether the function turns return or False.
- If it returns true then print the itrnumb.
- The Exit of the Program.
Below is the implementation:
# function which returns true if the given number is # perfect number else it will return False def checkPerfectNumbr(givenNumb): # Taking a variable totalSum and initializing it with 1 totalSum = 1 # Iterating from 2 to n-1 for i in range(2, givenNumb): # if the iterator value is divides the number then add the given number to totalSum if givenNumb % i == 0: totalSum += i # if the totalSum is equal to the given number # then it is perfect number else it is not perfect number if(totalSum == givenNumb): # if it is true then it is perfect number then return true return True # if nothing is returned then it is not a perfect number so return False return False # Give the lower limit range and upper limit range as # user input using map(),int(),split() functions. # Store them in two separate variables. lowlimrange, upplimrange = map(int, input( 'Enter lower limit range and upper limit range separate by spaces = ').split()) print('The Perfect numbers in the given range', lowlimrange, 'and', upplimrange, 'are:') # Loop from lower limit range to upper limit range using For loop. for itrvalue in range(lowlimrange, upplimrange+1): # Inside the for loop pass the iterator value to checkNeonnumb() function. if(checkPerfectNumbr(itrvalue)): # If it returns true then print the iterator value. print(itrvalue, end=' ')
Output:
Enter lower limit range and upper limit range separate by spaces = 125 8592 The Perfect numbers in the given range 125 and 8592 are: 496 8128
Here we printed all the perfect numbers in the range 125 to 8592
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.