{"id":16332,"date":"2021-08-17T08:48:27","date_gmt":"2021-08-17T03:18:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=16332"},"modified":"2021-11-22T18:37:22","modified_gmt":"2021-11-22T13:07:22","slug":"python-program-to-generate-perfect-numbers-in-an-interval","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-generate-perfect-numbers-in-an-interval\/","title":{"rendered":"Python Program to Generate Perfect Numbers in an Interval"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Add Two Numbers Without Using the \u201c+\u201d Operator.<\/a> 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.<\/p>\n 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\u2019s construct a Python program to determine whether or not a number is a perfect number. Let\u2019s write some Python code to see if the given user input is a perfect number or not, and have some fun with Python coding.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to generate Perfect Numbers in a given interval.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Here we printed all the perfect numbers in the range 125 to 8592<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Add Two Numbers Without Using the \u201c+\u201d Operator. If the sum of a number\u2019s 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 …<\/p>\n
\nIf the sum of a number\u2019s appropriate divisors (excluding the number itself) equals the number, the number is said to be the perfect number.<\/p>\nGiven lower limit range = 1\r\nGiven upper limit range=1000<\/pre>\n
The Perfect numbers in the given range 1 and 1000 are:\r\n1 6 28 496<\/pre>\n
Given lower limit range = 496\r\nGiven upper limit range=8128<\/pre>\n
The Perfect numbers in the given range 125 and 8592 are:\r\n496 8128<\/pre>\n
Program to Generate Perfect Numbers in an Interval<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# function which returns true if the given number is\r\n# perfect number else it will return False\r\n\r\n\r\ndef checkPerfectNumbr(givenNumb):\r\n # Taking a variable totalSum and initializing it with 1\r\n totalSum = 1\r\n # Iterating from 2 to n-1\r\n for i in range(2, givenNumb):\r\n # if the iterator value is divides the number then add the given number to totalSum\r\n if givenNumb % i == 0:\r\n totalSum += i\r\n\r\n # if the totalSum is equal to the given number\r\n # then it is perfect number else it is not perfect number\r\n\r\n if(totalSum == givenNumb):\r\n # if it is true then it is perfect number then return true\r\n return True\r\n # if nothing is returned then it is not a perfect number so return False\r\n return False\r\n\r\n\r\n# Give the lower limit range as static input and store it in a variable.\r\nlowlimrange = 1\r\n# Give the upper limit range as static input and store it in another variable.\r\nupplimrange = 1000\r\nprint('The Perfect numbers in the given range',\r\n lowlimrange, 'and', upplimrange, 'are:')\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor itrvalue in range(lowlimrange, upplimrange+1):\r\n # Inside the for loop pass the iterator value to checkNeonnumb() function.\r\n if(checkPerfectNumbr(itrvalue)):\r\n # If it returns true then print the iterator value.\r\n print(itrvalue, end=' ')\r\n<\/pre>\n
The Perfect numbers in the given range 1 and 1000 are:\r\n1 6 28 496<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# function which returns true if the given number is\r\n# perfect number else it will return False\r\n\r\n\r\ndef checkPerfectNumbr(givenNumb):\r\n # Taking a variable totalSum and initializing it with 1\r\n totalSum = 1\r\n # Iterating from 2 to n-1\r\n for i in range(2, givenNumb):\r\n # if the iterator value is divides the number then add the given number to totalSum\r\n if givenNumb % i == 0:\r\n totalSum += i\r\n\r\n # if the totalSum is equal to the given number\r\n # then it is perfect number else it is not perfect number\r\n\r\n if(totalSum == givenNumb):\r\n # if it is true then it is perfect number then return true\r\n return True\r\n # if nothing is returned then it is not a perfect number so return False\r\n return False\r\n\r\n\r\n# Give the lower limit range and upper limit range as\r\n# user input using map(),int(),split() functions.\r\n# Store them in two separate variables.\r\nlowlimrange, upplimrange = map(int, input(\r\n 'Enter lower limit range and upper limit range separate by spaces = ').split())\r\nprint('The Perfect numbers in the given range',\r\n lowlimrange, 'and', upplimrange, 'are:')\r\n# Loop from lower limit range to upper limit range using For loop.\r\nfor itrvalue in range(lowlimrange, upplimrange+1):\r\n # Inside the for loop pass the iterator value to checkNeonnumb() function.\r\n if(checkPerfectNumbr(itrvalue)):\r\n # If it returns true then print the iterator value.\r\n print(itrvalue, end=' ')\r\n<\/pre>\n
Enter lower limit range and upper limit range separate by spaces = 125 8592\r\nThe Perfect numbers in the given range 125 and 8592 are:\r\n496 8128<\/pre>\n
\n