{"id":16671,"date":"2021-08-12T09:25:13","date_gmt":"2021-08-12T03:55:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=16671"},"modified":"2021-11-22T18:38:33","modified_gmt":"2021-11-22T13:08:33","slug":"python-program-to-find-strong-numbers-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-strong-numbers-in-a-list\/","title":{"rendered":"Python Program to Find Strong Numbers in a List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Generate Strong Numbers in an Interval<\/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 list, and the task is to find all the Strong numbers in a given list.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find all the Strong numbers in a given list.<\/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 Generate Strong Numbers in an Interval 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 ! = 1 …<\/p>\n
\nStrong number:<\/strong><\/p>\nGiven List = [4, 1, 4, 145]<\/pre>\n
The Strong Numbers in a given List are :\r\n1 145<\/pre>\n
Given List =[5, 10, 650, 40585, 2, 145, 900]<\/pre>\n
The Strong Numbers in a given List are :\r\n40585 2 145<\/pre>\n
Program to Find Strong Numbers in a List<\/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 list as static input and store it in a variable.\r\nlist1 = [4, 1, 4, 145]\r\n# Loop in the above given list using For Loop.\r\nprint(\"The Strong Numbers in a given List are :\")\r\nfor i in list1:\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 = i\r\n # using while to extract digit by digit of the given iterator value\r\n while(i):\r\n s = 1\r\n factNum = 1\r\n # Getting the last digit of the iterator value\r\n remainder = i % 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 i = i\/\/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 List are :\r\n1 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 list as user input using list(),map(),input(),and split() functions and \r\n#Store it in a variable\r\nlist1 = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Loop in the above given list using For Loop.\r\nprint(\"The Strong Numbers in a given List are :\")\r\nfor i in list1:\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 = i\r\n # using while to extract digit by digit of the given iterator value\r\n while(i):\r\n s = 1\r\n factNum = 1\r\n # Getting the last digit of the iterator value\r\n remainder = i % 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 i = i\/\/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 List Elements separated by spaces = 1 145 10 15 2\r\nThe Strong Numbers in a given List are :\r\n1 145 2<\/pre>\n
\n