简体   繁体   English

打印所有 n 位数字,其数字的乘积等于给定的产品 python

[英]Print all n-digit numbers whose product of digits equals to given product python

enter image description here Display 3-digit prime numbers that have the product of digits equal to a given p-value.在此处输入图像描述显示 3 位素数,这些素数的数字乘积等于给定的 p 值。 Example: For p = 9 the numbers 191, 313, 331, 911 meet the conditions of the problem.示例:对于 p = 9,数字 191、313、331、911 满足问题的条件。

I found a code that does this just to find out their amount but not the product.我找到了一个代码,它只是为了找出它们的数量而不是产品。 I need to find out what I change in the program to find out the product, not the amount.我需要找出我在程序中更改的内容以找出产品,而不是数量。

Here is the code to be solved in python:下面是python中要解决的代码:

def findNDigitNumsUtil(n, product, out,index):
    # Base case
    if (index > n or product < 0):
        return
    f = ""
    # If number becomes N-digit
    if (index == n):
        # if sum of its digits is equal
        # to given sum, print it
        if(product == 0):
            out[index] = "\0"
            for i in out:
                f = f + i
            print(f, end = " ")
        return
    # Traverse through every digit. Note
    # that here we're considering leading
    # 0's as digits
    for i in range(10):
        # append current digit to number
        out[index] = chr(i + ord('0'))
        # recurse for next digit with reduced sum
        findNDigitNumsUtil(n, product - i,
                        out, index + 1)

# This is mainly a wrapper over findNDigitNumsUtil.
# It explicitly handles leading digit
def findNDigitNums( n, sum):
    # output array to store N-digit numbers
    out = [False] * (n + 1)
    # fill 1st position by every digit
    # from 1 to 9 and calls findNDigitNumsUtil()
    # for remaining positions
    for i in range(1, 10):
        out[0] = chr(i + ord('0'))
        findNDigitNumsUtil(n, sum - i, out, 1)
# Driver Code
if __name__ == "__main__":
    n = 3
    sum = 9
    findNDigitNums(n, sum)

# This code is contributed
# by ChitraNayal

Disclaimer: this code was written by Github Copilot with minimal supervision.免责声明:此代码由 Github Copilot 在最少监督下编写。

# Display 3-digit prime numbers that have the product of digits equal to a given p-value.
# Example: For p = 9 the numbers 191, 313, 331, 911 meet the conditions of the problem.

def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

def print_3_digit_primes(p):
    for i in range(100, 1000):
        if is_prime(i) and (i % 10) * (i // 100) * (i % 100 // 10) == p:
            print(i)

print_3_digit_primes(9)

暂无
暂无

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

相关问题 最大回文集,是两个n位数字的乘积(Python) - Largest palindrome which is product of two n-digit numbers (Python) 如何形成给定数字的所有可能组合以形成不重复数字的n位数字?(python) - How to form all possible combination of given digits to form a n-digit number without repetition of digits?(python) 在一个数字序列中找到最大的 n 位乘积 - Finding the largest n-digit product in a sequence of numbers 我如何优化这个递归 function 来计算其数字等于总和的 n 位十六进制数的数量? - How do I optimize this recursive function that counts the number of n-digit hexadecimal numbers whose digits equal a sum? Python 3 - 递归查找具有和不具有唯一数字的 n 位代码 - Python 3 - Recursively finding n-digit code with and without unique digits 详细解释“相邻位数绝对差不超过K的N位数字的计数”的逻辑 - Explain the logic of 'Count of N-digit numbers with absolute difference of adjacent digits not exceeding K' in Detail 重新模式以匹配非数字文本前面的所有 n 位数字 - re pattern to match all n-digit numbers in front of non-digit text Python:获取最大的 n 位数 - Python: Getting largest n-digit number 给定一个由 n 个整数组成的数组。 打印其数组值乘积的最后两位数字 - You are given an array consisting of n integers. Print the last two digits of the product of its array values 如何在Python中打印右对齐的货币符号和n位十进制数字 - How to print currency symbol and an n-digit decimal number right aligned in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM