如何解决寻找最小数==给定数的阶乘和时的输出奇怪
#Function factorial
def fact(d):
f=1
for i in range(d,-1):
f=f*i
print(f"factorial {f}")
return f
#Function for summation of factorial of digits
def f(n):
s=0
d=n%10
s=fact(d)+s
n=int(n/10)
print(f"summing {s}")
return s
l=[]
q=int(input("enter number of queries"))
print(q)
n=int(input("enter the number to which you want to calculate"))
m=int(input("enter range"))
for i in range(1,n+1):
l.append(i) #adding elements from 1 to n in list
print(l[i-1])
for j in range(1,m+1):
p=f(j)
if(l[i-1]==p):#element in list is equal to function (i.e sum of factorial of digits)
l[i-1]=p #then assign p to list
print(f"list {l[i-1]}")
break #then break the second loop
例如: 对于查询1 n = 3和m = 100
直到1到n
在m中查找其阶乘之和等于n中的数字的数字
例如: 5 = 25(如2!+ 5!= 2+ 120 = 122 1 + 2 + 2 = 5)
然后中断下一个i迭代,但我不知道我在哪里犯错。
解决方法
目标:找到最小的x
,以使x
的数字的阶乘之和为n
。
示例行为:
Find the smallest x such that:
the sum of digits of the factorials of the digits of x is n
Please provide n: 12
Please provide the maximal x to check: 10000
Trying 1:
Sum of the digits of factorials of the digits of 1 is:
digit_sum(1!)
= digit_sum(1)
= 1
...
Trying 4:
Sum of the digits of factorials of the digits of 4 is:
digit_sum(4!)
= digit_sum(24)
= 6
...
Trying 16:
Sum of the digits of factorials of the digits of 16 is:
digit_sum(1!) + digit_sum(6!)
= digit_sum(1) + digit_sum(720)
= 10
...
Trying 33:
Sum of the digits of factorials of the digits of 33 is:
digit_sum(3!) + digit_sum(3!)
= digit_sum(6) + digit_sum(6)
= 12
x is 33.
源代码:
import math
def print_sum_eq(x):
print(f" Sum of digits of the factorials of the digits of {x} is:")
msg1 = [f"digit_sum({d}!)" for d in str(x)]
print(" " + " + ".join(msg1))
msg2 = [f"digit_sum({math.factorial(int(d))})" for d in str(x)]
fact_values_str = " + ".join(msg2)
print(f" = {fact_values_str}")
def digit_sum(x):
return sum(int(d) for d in str(x))
def sum_fact_digit(x):
"""Calculate sum of factorials of the digits of x
For example,when x = 25,the digits are 2 and 5. The sum of the
factorials of the digits is 2! + 5! = 2 + 120 = 122.
Parameters
----------
x : int
Returns
-------
digit_sum : int
Sum of the factorials of the digits of x
"""
s = 0
print_sum_eq(x)
# Loop over the digits of x
for d in str(x):
digit = int(d)
digit_fact = math.factorial(digit)
s += digit_sum(digit_fact)
print(f" = {s}")
return s
def search(n,max_x=None):
"""Try to find x such that sum of factorials of the digits of x is n
Parameters
----------
n : int
max_x : int,optional
The function will search over x = 1,2,...,max_x
Returns
-------
result : int or None
If we find x,the result is x.
If we can't find x,the result is None.
"""
if max_x is None:
max_x = int(10 ** n)
for x in range(1,max_x + 1):
print(f"Trying {x}:")
sum = sum_fact_digit(x)
if sum == n:
return x
return None
def main():
print("Find the smallest x such that:")
print(" the sum of digits of the factorials of the digits of x is n")
n = int(input("Please provide n: "))
max_x = int(input("Please provide the maximal x to check: "))
x = search(n,max_x=max_x)
if x is None:
print("Cannot find such a x.")
else:
print(f"x is {x}.")
main()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。