{"id":16325,"date":"2021-08-12T09:25:16","date_gmt":"2021-08-12T03:55:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=16325"},"modified":"2021-11-22T18:38:32","modified_gmt":"2021-11-22T13:08:32","slug":"python-program-to-generate-strong-numbers-in-an-interval","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-generate-strong-numbers-in-an-interval\/","title":{"rendered":"Python Program to Generate Strong Numbers in an Interval"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Select a Random Element from a Tuple<\/a> A Strong number is a special number in which the total of all digit factorials equals the number itself.<\/p>\n Ex: 145 the sum of factorial of digits = 1 ! + 4 ! +5 ! = 1 + 24 +125<\/p>\n To determine whether a given number is strong or not. We take each digit from the supplied number and calculate its factorial, we will do this for each digit of the number.<\/p>\n We do the sum of factorials once we have the factorial of all digits. If the total equals the supplied number, the given number is strong; otherwise, it is not.<\/p>\n Given a number, the task is to check whether the given number is strong number or not.<\/p>\n Examples:<\/strong><\/p>\n Example 1:<\/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 Strong 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 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 Select a Random Element from a Tuple Strong number: A Strong number is a special number in which the total of all digit factorials equals the number itself. Ex: 145 the sum of factorial of digits = 1 ! + 4 ! +5 ! = …<\/p>\n
\nStrong number:<\/strong><\/p>\nGiven lower limit range = 1\r\nGiven upper limit range= 200<\/pre>\n
The Strong Numbers in a given range 1 and 200 are :\r\n1 2 145<\/pre>\n
Given lower limit range = 100\r\nGiven upper limit range= 60000<\/pre>\n
The Strong Numbers in a given range 100 and 60000 are :\r\n145 40585<\/pre>\n
Program to Generate Strong Numbers in an Interval<\/h2>\n
\n
Method #1: Using While loop and factorial() function (Static input)<\/h3>\n
\n
# Import the math module using import keyword.\r\nimport math\r\n# Give the lower limit range as static input and store it in a variable.\r\ngvn_lower_lmt = 1\r\n# Give the upper limit range as static input and store it in another variable.\r\ngvn_upper_lmt = 200\r\n# Loop from lower limit range to upper limit range using For loop.\r\nprint(\"The Strong Numbers in a given range\",\r\n gvn_lower_lmt, \"and\", gvn_upper_lmt, \"are :\")\r\nfor itr in range(gvn_lower_lmt, gvn_upper_lmt+1):\r\n # Taking a variable totalSum and initializing it with 0\r\n totalSum = 0\r\n # Put the iterator value in a temporary variable called tempNum.\r\n tempNum = itr\r\n # using while to extract digit by digit of the given iterator value\r\n while(itr):\r\n s = 1\r\n factNum = 1\r\n # Getting the last digit of the iterator value\r\n remainder = itr % 10\r\n # calculating the factorial of the digit(extracted by remainder variable)\r\n # using math.fatorial function\r\n factNum = math.factorial(remainder)\r\n # Adding the factorial to the totalSum\r\n totalSum = totalSum + factNum\r\n # Dividing the given itertor value by 10\r\n itr = itr\/\/10\r\n # checking if the totalSum is equal to the iterator value\r\n # if it is true then it is strong number then return true\r\n if(totalSum == tempNum):\r\n print(tempNum, end=' ')\r\n<\/pre>\n
The Strong Numbers in a given range 1 and 200 are :\r\n1 2 145<\/pre>\n
Method #2: Using While loop and factorial() function (User input)<\/h3>\n
\n
# Import the math module using import keyword.\r\nimport math\r\n#Give the lower limit range as user input using int(input()) and\r\n#store it in a variable.\r\ngvn_lower_lmt = int(input(\"Enter some random number = \"))\r\n#Give the upper limit range as user input using int(input()) and \r\n#store it in another variable.\r\ngvn_upper_lmt = int(input(\"Enter some random number = \"))\r\n# Loop from lower limit range to upper limit range using For loop.\r\nprint(\"The Strong Numbers in a given range\",\r\n gvn_lower_lmt, \"and\", gvn_upper_lmt, \"are :\")\r\nfor itr in range(gvn_lower_lmt, gvn_upper_lmt+1):\r\n # Taking a variable totalSum and initializing it with 0\r\n totalSum = 0\r\n # Put the iterator value in a temporary variable called tempNum.\r\n tempNum = itr\r\n # using while to extract digit by digit of the given iterator value\r\n while(itr):\r\n s = 1\r\n factNum = 1\r\n # Getting the last digit of the iterator value\r\n remainder = itr % 10\r\n # calculating the factorial of the digit(extracted by remainder variable)\r\n # using math.fatorial function\r\n factNum = math.factorial(remainder)\r\n # Adding the factorial to the totalSum\r\n totalSum = totalSum + factNum\r\n # Dividing the given itertor value by 10\r\n itr = itr\/\/10\r\n # checking if the totalSum is equal to the iterator value\r\n # if it is true then it is strong number then return true\r\n if(totalSum == tempNum):\r\n print(tempNum, end=' ')\r\n<\/pre>\n
Enter some random number = 1\r\nEnter some random number = 5000\r\nThe Strong Numbers in a given range 1 and 5000 are :\r\n1 2 145<\/pre>\n
\n