This Python code example illustrates how to determine whether a given number is a “spy number.” A spy number is a number where the sum of its digits is equal to the product of its digits. The code takes an input number, processes its digits, and then checks if it meets the criteria of a spy number.
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")
checkSpy(number)
The function checkSpy
takes a single integer argument, number
, and determines whether it is a spy number.
def checkSpy(number):
sum, mul = 0, 1
sum
and mul
, are initialized to 0 and 1 respectively. sum
will store the sum of the digits, and mul
will store the product of the digits.while number > 0:
A while
loop is used to process each digit of the number.
while number > 0:
rem = number % 10
sum += rem
mul *= rem
number = int(number / 10)
rem = number % 10
extracts the last digit of number
.sum
and multiplied with mul
.number = int(number / 10)
removes the last digit of number
.if sum == mul:
After processing all digits, the function checks if the sum of the digits equals the product of the digits.
if sum == mul:
return True
else:
return False
True
if the number is a spy number and False
otherwise.The main code segment takes an integer input from the user and calls the checkSpy
function.
number = int(input("Enter a number to check for spy number: "))
The result of the checkSpy
function is used to print whether the number is a spy number.
if checkSpy(number) == True:
print(str(number) + " is a spy number")
else:
print(str(number) + " is not a spy number")
This Python code example demonstrates how to find and print all spy numbers up to a given upper limit. A spy number is defined as a number where the sum of its digits equals the product of its digits. The script includes a function to check if a number is a spy number and uses a loop to find all such numbers within the specified range. This example highlights the use of loops, conditional statements, and basic arithmetic operations in Python.
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=" ")
The main code segment takes an integer input from the user and calls the checkSpy
function for each number up to the specified limit.
max = int(input("Enter the upper bound to find spy numbers: "))
max
.for i in range(1, max):
A for
loop iterates through each number from 1
to max - 1
.
print("Spy numbers: ")
for i in range(1, max):
if checkSpy(i):
print(i, end=" ")
i
in the range from 1
to max - 1
.checkSpy
function is called for each i
.checkSpy(i)
returns True
, i
is printed as a spy number, with numbers separated by spaces.