Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找最小数字的阶乘之和==给定数字时的奇怪输出_Python_Algorithm - Fatal编程技术网

Python 查找最小数字的阶乘之和==给定数字时的奇怪输出

Python 查找最小数字的阶乘之和==给定数字时的奇怪输出,python,algorithm,Python,Algorithm,例如: 查询1 n=3,m=100 直到1到n 在m中查找数字的阶乘和等于n中数字的和的数字 例如: 5=25(等于2!+5!=2+120=122 1+2+2=5) 然后继续下一次迭代,但我不知道我在哪里犯了错误。目标:找到最小的x,这样x的数字的阶乘位数之和就是n 示例行为: #Function factorial def fact(d): f=1 for i in range(d,0,-1): f=f*i print(f"factorial {f}")

例如: 查询1 n=3,m=100

直到1到n
在m中查找数字的阶乘和等于n中数字的和的数字

例如: 5=25(等于2!+5!=2+120=122 1+2+2=5)


然后继续下一次迭代,但我不知道我在哪里犯了错误。目标:找到最小的
x
,这样
x
的数字的阶乘位数之和就是
n

示例行为:

#Function factorial
def fact(d):
  f=1
  for i in range(d,0,-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
  
源代码:

导入数学
def打印和等式(x):
print(f“{x}的数字的阶乘的数字之和:”)
msg1=[f“数字总和({d}!)”表示str(x)中的d)]
打印(“+”+“.join(msg1))
msg2=[f”str(x)中d的数字和({math.factorial(int(d))})”
事实值\u str=“+”.join(msg2)
打印(f“={fact\u values\u str}”)
定义数字和(x):
返回和(整数(d)表示str(x)中的d)
定义和事实数字(x):
“”“计算x位数的阶乘和
例如,当x=25时,数字为2和5
数字的阶乘为2!+5!=2+120=122。
参数
----------
x:int
退换商品
-------
数字和:int
x的数字的阶乘之和
"""
s=0
打印求和公式(x)
#在x的数字上循环
对于str(x)中的d:
数字=int(d)
数字\事实=数学阶乘(数字)
s+=数字和(数字事实)
打印(f“={s}”)
返回s
def搜索(n,max_x=None):
“”“尝试查找x,使x的位数的阶乘之和为n。”
参数
----------
n:int
max_x:int,可选
该函数将搜索x=1,2,…,max_x
退换商品
-------
结果:int或None
如果我们找到x,结果就是x。
如果我们找不到x,结果是没有。
"""
如果max_x为无:
最大x=int(10*n)
对于范围内的x(1,最大x+1):
打印(f“Trying{x}:”)
总和=总和\事实\数字(x)
如果总和=n:
返回x
一无所获
def main():
打印(“找到最小的x,以便:”)
打印(“x的数字的阶乘的数字之和为n”)
n=int(输入(“请提供n:”)
max_x=int(输入(“请提供要检查的最大x:”)
x=搜索(n,max\ux=max\ux)
如果x为无:
打印(“找不到这样的x”)
其他:
print(f“x是{x}.”)
main()

请在代码中添加注释。例如:对于查询1,第一个循环中的n=3和m=100 i=1,然后第二个循环运行到m=100 1=1(1!)中断第二个循环i=2,然后第二个循环运行到m=100 2=1(1!)2=2(2!),然后中断第二个循环i=3,然后循环运行到m=100 3=13=3。。。3=12(如(1!+2!)=3),然后断开第二个循环编号,最小编号。如12=1+2!=3 , 25=2!+5!=2+120=122=1+2+2=5所以12=3和25=5这是我想要检查我的评论,例如最小数,其数字的阶乘之和是的,这是范围,这可以做得更快。祝你好运,你的方法发现319的答案是13369999999999。@B问题:试图猜测op真正想要什么花了太多时间:)你如何让它更快?解决整数规划问题通常需要使用hueristic,这并不保证找到最优答案。我很想知道你的方法。你可以使用动态规划。我们知道小数字将先于大数字,我们希望使用尽可能少的数字。这意味着我们可以使用广度优先搜索来建立最后一项的dict
last\u digit\u in\u sum
,在最短的(阶乘的位数之和)和中,该数字可以累加到任何特定的数字。然后从你的目标号码向后遍历数据结构,得到数字。如何修剪搜索树?每个节点对于10位数字中的每一位都有一个分支。这是一个10维网格上的动态规划。它是一个简单的广度优先搜索,只有在没有其他数据首先到达时,您才向
todo
列表中添加一个数字。自动运行它。
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.