Define f(n) as the sum of the factorials of the digits of n. For example, f(342) = 3! + 4! + 2! = 32.
Define sf(n) as the sum of the digits of f(n). So sf(342) = 3 + 2 = 5.
Define g(i) to be the smallest positive integer n such that sf(n) = i. Though sf(342) is 5, sf(25) is also 5, and it can be verified that g(5) is 25.
Define sg(i) as the sum of the digits of g(i). So sg(5) = 2 + 5 = 7.
Further, it can be verified that g(20) is 267 and ∑ sg(i) for 1 ≤ i ≤ 20 is 156.
What is ∑ sg(i) for 1 ≤ i ≤ 150?
![](https://static.wixstatic.com/media/cc6ad7_a6ddc538ae6548b480b119cd5ab02b11~mv2.jpg/v1/fill/w_980,h_653,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/cc6ad7_a6ddc538ae6548b480b119cd5ab02b11~mv2.jpg)
def factorial(n):
if n == 1:
return n
elif n < 1:
return 0
else:
return n*factorial(n-1)
def f(x):
x = [int(i) for i in str(x)]
sum = 0
for j in range(len(x)):
sum = sum + factorial(x[j])
return sum
def sf(x):
x = f(x)
x = [int(i) for i in str(x)]
sum = 0
for j in range(len(x)):
sum = sum + int(x[j])
return sum
def g(x):
count = 0
while sf(count) < x:
count += 1
return count
def sg(x):
l = g(x)
y = [int(i) for i in str(l)]
sum = 0
for i in range(len(y)):
sum = sum + y[i]
return sum
def finalanswer(n):
answer = 0
for i in range(n):
answer = answer + sg(i)
return(answer)
Comments