python - 寻找最小数字中数字的阶乘总和时的奇怪输出==给定数字
问题描述
#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
例如:对于查询 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()
推荐阅读
- angular - Ngb等级最大值动态变化不反映在星星长度中
- android - 如果单元测试失败,SonarQube 不使用 Jacoco 报告
- security - 如何在 https 请求中使用 http 请求?
- javascript - VueJS - 设置 CSS 类来回答问题
- python-3.x - 如何以列表形式查看 GUI 上的 EXIF 数据?
- php - htaccess 将域前缀 abc.domainname.com 重写为 xyz.domainname.com
- javascript - 我做错了什么,我有这个类型错误:无法设置未定义的属性“可见性”?
- c# - 使用循环 ASP.NET Core MVC 呈现多个图表
- python - 如何从 python 在 mysql 中创建存储过程?
- azure - pod 在 azure 上的 Statefulset 部署上具有未绑定的即时 PersistentVolumeClaims