enter image description hereDisplay 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.
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:
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
CodePudding user response:
Disclaimer: this code was written by Github Copilot with minimal supervision.
# 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)