Python : Curious Number
Python: Curious Number
Problem No.34 in ProjectEuler
Definition: A number is Curious Number if the factorial of their digits equal to the number itself.
Example: 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Our Task: We will write two functions, first one will get (return) all digits in the number, then another function to get the factorial of each digits in that number then with If statement we will examine the result.Enhancement: We will ask the user to enter a number and we will check if it is a Curious Number.
We will reuse some of our functions that we wrote in previous posts.
The Code:
digs=[]
print(‘\nEnter a number to see if it is a Curious Number or not.’)
num=input (‘\nEnter a number: ‘)
num=input (‘Enter a number: ‘)
tot=0
# To get the digits In a number
def digits_in_num (num):
for each in str(num):
digs.append(each)
# To get the Factorial of a number
def Factorial_digit_sum(num):
if (num == 0) :
return 1
else:
return num * Factorial_digit_sum(num-1)
for each in digs:
print(‘factorial :’,each,’ is ‘,Factorial_digit_sum(int(each)))
tot = tot + Factorial_digit_sum(int(each))
print(‘\nTotal sum of the Factorial of each digits is: ‘,tot)
if int(num) == tot:
print(num ,’Is a Curious Number.’)
else:
print(num ,’Is NOT Curious Number.’)