×

Python Strong Number

Strong Number- Logic

  • Divide each of the number's digits into separate units to determine if it is a strong number or not.
  • The factorial of each digit must then be determined. The values produced for each digit following the factorial operation will then be added.
  • In the end, the sum needs to be compared to the supplied number. The number is a strong one if the response is yes.

Algorithm

To determine whether a given number is a strong number or not, apply the algorithm provided below.

  • Obtain the required number and use it as input to determine if it is a strong number or not. Let's assign this input number to the variable N.
  • Put N's value in a short-term variable called temp_N.
  • Put a variable's value, such as "sum," to zero. This will record the factorial sum for every digit of N.
  • The last digit of the number needs to be preserved in a variable, such as the last digit = N % 10.
  • It is necessary to keep the factorial of the last digit in a variable called fact.
  • Every time the factorial of the last digit is found, the sum should be raised.
    sum = sum + fact
  • The number must be divided by 10 to become smaller in units after each factorial operation.
    N=N/10
  • Until N > 0, then repeat steps 4 through 7.
  • When the loop is finished, the condition sum = temp N should be tested to see if the sum equals the number. If the answer is affirmative, the input number is a strong one.

Strong Number Program Implementation in Python

The algorithm described above is used to build the following Python computer code, which enables us to detect whether a given integer is a strong number or not.

Strong Number with while Loop in Python

Example 1:

sum = 0
N = int(input("Enter a number: "))
temp_N = N
while(N):
    k = 1
    fact = 1
    r = N % 10
while(k <= r):
        fact = fact * k
        k = k + 1
    sum = sum + fact
    N = N//10
if(sum == temp_N):
    print(str(temp_N) + " is a strong number")
else:
    print(str(temp_N) + " is not a strong number")

Output:

Strong Number in Python
  • The user is initially prompted for input when the program is run. We can pass the necessary number to determine if it is a strong number or not. In this instance, we entered after passing 145.
  • The response is "145 is a strong number," which is the appropriate output. This is demonstrated in the screenshot above.
  • Passing another input now. Once more, we exceeded 163. Since 163 is a weak number, we obtained the results depicted below.
Strong Number in Python

The software code that we just looked at conducts a simple number check. We might occasionally need to confirm this for several numbers at once.

Finding Strong Numbers in a List Using a Python Program

We'll employ another Python method for this. We can do this with the aid of the Python program code listed below. Examine the Python programming code that follows carefully to observe how each component carries out its intended function.

Example 2:

# Compute factorial
def factorial1(N): 
if(N == 0 or N == 1): 
        fact = 1
    else: 
        fact = N * factorial1(N - 1) 
    return fact 
# Check for strong number 
def strong_number(list): 
    n_list =[]   
    for i in list: 
        temp_N = i 
        sum = 0
        while(temp_N): 
            r = temp_N % 10
            sum += factorial1(r) 
            temp_N = temp_N // 10
if(sum == i): 
            n_list.append(i) 
        else: 
            pass                
    return n_list           
# Input
a = [10, 25, 2, 121, 145, 786, 49, 1, 20900] 
strong_num_list = strong_number(a) 
print(strong_num_list)

Output:

Strong Number in Python
  • The Python program code presented above is divided into three sections. Firstly the fact N() function, which determines the factorial of the parameterized number. Second, there is the strong number() function, which adds up each digit's factorial sum in the given number.
  • The strong_number() function accepts a list as a parameter since we want to list several numbers as input. The strong_number function first creates a bare list called n list.
  • A while loop computes the factorial total for each digit. The method then returns the value if the sum does equal the supplied number.
  • The input portion is the third component of the program. Simply giving the necessary values and running the application will allow us to pass them.
  • The list is given to the strong number function, which returns one or more strong number(s) stored in the list variable strong_num_list.
  • Finally, we output the list of all potential string values.

From the input list of values, as shown above, we obtained an output list that contained the values 2, 145 and 1. The result is precise. Let's run the program once more with a new set of parameters.

Example 3:

#  Factorial Computation
def factorial1(Number): 
if(Number == 0 or Number == 1): 
        fact = 1
    else: 
        fact = Number * factorial1(N - 1) 
    return fact 
# Check for strong number 
def strong_number(list): 
    n_list =[]   
    for i in list: 
        temp_N = i 
        sum = 0
        while(temp_N): 
            r = temp_N % 10
            sum += factorial1(r) 
            temp_N = temp_N // 10
if(sum == i): 
            n_list.append(i) 
        else: 
pass                
    return n_list           
# Input
a = [67, 87, 13, 949, 71, 890, 1018, 73, 1, 0] 
strong_num_list = strong_number(a) 
print(strong_num_list)

Output:

Strong Number in Python

As seen above, a list with the values 1 and 0 as output has been returned. Consequently, the values on the list are strong numbers.

  • Now, lets pass a different list of values to the above written code.
input_vals = [67, 87, 13, 949, 71, 890, 1018, 73, 11, 90]

Output Obtained

Strong Number in Python

The output has been returned as a blank list, as seen above. As a result, none of the values in the list are considered to be strong numbers.

Strong Number with for Loop in Python

Example:

sum = 0
number=int(input("enter a number:"))
temp = number
while(temp > 0):
   fact = 1
   rem = temp % 10
   for i in range(1, rem + 1):
      fact = fact * i
print("Factorial of %d = %d " %(rem, fact))
   sum = sum +fact
   temp = temp // 10


print("Sum of factorials of the number %d = %d " %(number,sum))
if(sum == number):
print("Strong Number")
else:
print("Not a strong number")

Output:

Strong Number in Python

Strong Number Python Program Employing Function

To find the strong number, we employ a built-in Python function.

Example:

import math
number = int(input("Enter Number: "))
sum = 0
temp = number
while(temp>0):
   remainder = temp % 10
   fact = math.factorial(remainder)
print("Factorial of %d = %d" %(remainder, fact))
   sum = sum + fact
   temp = temp // 10


print("Sum of factorials of a number %d = %d" %(number,sum))
if(sum == number):
print("Strong number")
else:
print("not a strong number")

Output

Strong Number in Python

We have imported math since we are using a built-in function named factorial() found in the math library. The factorial of a single digit was calculated using this factorial() alone. As a result, it saves us from having to repeatedly calculate the factorial of each digit.

Conclusion

An idea in mathematics known as a strong number has several uses. Strong numbers are particular numbers where the sum of all digit factorials equals the sum itself. Python's numerous features enable us to swiftly and efficiently construct reliable number-checking applications.


Related Topics

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

Is Python Case Sensitive

Case sensitivity is the mode of dealing with the written alphabet. The cases of the alphabet are examined and based on these words are being treated. The uppercase and lowercase...

3 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Flutter with tensor flow in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

3 minutes read.

Python History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

Python Memory Management

In order to manage memory, Python uses a private heap that contains all of its data structures and objects. The Python memory manager is responsible for the internal management of...

11 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

Python Namespace

In python, the namespace is a very important concept that should be understood before using any function or variables. When we write code, we often use variables, libraries, functions, modules, etc....

4 minutes read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Difference between Python 2 and Python 3

In this tutorial, we will learn the differences between two versions of python, that is, python version 2 and python version 3. Some basic differences include- Python 2 is the older version...

3 minutes read.

Python Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

GUI Calendar using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

What is Sleeping Time in Python

Did you ever postpone a Python program's execution? Usually, you want your code to execute as quickly as possible. But there are times when it is in your best interests...

3 minutes read.