简体   繁体   English

寻找最小数字中的数字阶乘总和 == 给定数字时的奇怪输出

[英]Weird output when looking for sum of factorial of digits in smallest number == the given number

#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
  

Like for eg: For query 1 n= 3 and m=100例如:对于查询 1 n= 3 和 m=100

Till 1 to n直到 1 到 n
look in m for numbers whose sum of factorial of digits is equal to number in n在 m 中查找数字阶乘总和等于 n 中的数字的数字

For eg : 5=25 ( as 2! + 5! = 2+ 120 = 122 1+2+2=5)例如:5=25(如2!+5!=2+120=1221+2+2=5)

Then break for the next i iteration but I don't know where I'm making the mistake.然后在下一次迭代中休息,但我不知道我在哪里犯了错误。

Goal: Find the smallest x such that the sum of digits of the factorials of the digits of x is n .目标:找到最小的x ,使得的数字的阶乘的数字的总和xn

Sample behavior:示例行为:

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.

Source code:源代码:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM