Archive
Python: Digit Factorial Chains
Python: Digit factorial Chains
Problem No.74 @ ProjectEuler
In this task, we need to produce a chain of the summation of a factorial number and keep chasing each number in the chain until it starts repeat, we stop there and count the length of the chain, if its 60 then we increase a counter. ProjectEuler task 74 ask to do this and get all the numbers (How many are they?) below one Million (1000000) that has 60 numbers in it’s chain.
So we start to write two functions, one to get the factorial of a number, and the other to get the summation of the numbers in a list. Then with two (while) loop, an outer one to count the 60’s chain numbers, and an inner one to produce the chains.
At the end we manage to solve the problem 74 using Pythonanywhere.
![]() |
The Code:
#Digit factorial chains
#Problem 74
numbers=[]
# Function to get the Factorials of a number.
def get_factorial(num):
if (num == 0) :
return 1
else:
return num * get_factorial(num-1)
def get_summation(the_list):
tot=0
for each in the_list:
tot +=each
return tot
start_num = 2
the_num = start_num
keep_chain=[start_num]
chain_count = 0
print(‘We start with number ‘,start_num)
the_facto_d_sum = 0
while start_num < 1000000 :
the_num = start_num
keep_chain=[start_num]
while the_facto_d_sum != start_num:
for each in str(the_num):
numbers.append(get_factorial(int(each)))
the_facto_d_sum = get_summation(numbers)
the_num = the_facto_d_sum
if the_num in keep_chain :
the_facto_d_sum = start_num
else:
keep_chain.append(the_num)
numbers=[]
if len(keep_chain) == 60:
chain_count +=1
keep_chain =[]
start_num +=1
print(‘we have {} numbers with 60 ‘.format(chain_count))