Python Code Example: Spy number

A number is known as a spy number if the sum of its digits is exactly equal to the product of its digits.

Code Example 1

def checkSpy(number):
    sum, mul = 0, 1

    while number > 0:
        rem = number % 10
        sum += rem
        mul *= rem
        number = int(number / 10)

    if sum == mul:
        return True
    else:
        return False


number = int(input("Enter a number to check for spy number: "))

if checkSpy(number) == True:
    print(str(number) + " is a spy number")
else:
    print(str(number) + " is not a spy number")

Output

Enter a number to check for spy number: 123
123 is a spy number

Code Explanation

This code defines a function checkSpy that takes an integer number as input and returns a boolean value indicating whether number is a spy number or not.

The function starts by initializing two variables sum and mul to 0 and 1, respectively. These variables will be used to keep track of the sum and product of the digits of number.

The function then uses a while loop to repeatedly extract the last digit of number and update sum and mul accordingly. The while loop runs until number becomes 0.

For each iteration of the loop, the code uses the modulo operator (%) to extract the last digit of number, which is stored in the variable rem. The code then adds rem to sum and multiplies mul by rem. The code uses integer division (//) to remove the last digit of number, and the int function to convert the result to an integer.

After the while loop, the code uses an if statement to check if the sum of the digits of number is equal to their product. If sum is equal to mul, the code returns True, indicating that number is a spy number. If sum is not equal to mul, the code returns False, indicating that number is not a spy number.

The code then asks the user to input a number, and converts the input to an integer using the int function. The input value is then passed as an argument to the checkSpy function.

The code uses another if statement to check if the result of checkSpy is True. If checkSpy returns True, the code prints the message “X is a spy number”, where X is the value of number. If checkSpy returns False, the code prints the message “X is not a spy number”.

The str function is used to convert the value of number to a string representation, so that it can be concatenated with the string literals in the print statements.

This code demonstrates how to use a function, a while loop, and an if statement to determine if a number is a spy number, which is a number that has the sum of its digits equal to their product.

Code Example 2

This code defines a function checkSpy that takes in an integer number as an argument and returns True if the number is a spy number or False otherwise.

def checkSpy(number):
    sum, mul = 0, 1

    while number > 0:
        rem = number % 10
        sum += rem
        mul *= rem
        number = int(number / 10)

    if sum == mul:
        return True
    else:
        return False


max = int(input("Enter the upper bound to find spy numbers: "))

print("Spy numbers: ")
for i in range(1, max):
    if checkSpy(i):
        print(i, end=" ")

Output

Enter the upper bound to find spy numbers: 1500
Spy numbers: 
1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321 1124 1142 1214 1241 1412 1421

Code Explanation

The function first initializes two variables sum and mul to 0 and 1 respectively. Then, it uses a while loop to repeatedly divide the number by 10, until it becomes 0. At each iteration of the loop, the remainder of the division is found and added to the sum and multiplied with the mul. This gives us the sum and product of the digits of the number.

If the sum is equal to mul, the function returns True, otherwise it returns False.

The main part of the code then prompts the user to enter the upper bound max for finding spy numbers. The code then uses a for loop to iterate from 1 to max, and calls the checkSpy function for each number. If the result of the function is True, the number is printed as a spy number.

The end of the output is suppressed by setting end to a space character in the print statement, this ensures that the spy numbers are printed on the same line, separated by spaces.