列表的数字相乘 python
Multiply digits of number of list python
我有 3 位质数数组:
for num in range(100, 1000):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
numere_prime.append(num)
列表是:
numere_prime = [[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163...]
我需要的是将一个数字的每个数字相乘(例如 101 = 1 * 0 * 1),然后它显示所有 numere_prime 相乘后等于用户输入。
到目前为止我做了什么:
for elem in numere_prime:
digits = [int(x) for x in str(elem)] # split digits into [1,0,1][1,0,3]...
for n in list(digits):
// n * n_at_next_index * n_at_next_index
if //result of multiplying == //number set by input:
//append digits to result list
结果应该是这样的:input (9) = [191, 313, 331, 911]
你的素数算法是错误的。您不能仅在任何因素失败时追加到列表中,您必须知道所有因素都失败了。而且您不必一直搜索到 num
;您只需向上搜索 sqrt(num)
。由于最大值是 1000,您可以在 33 处停止。此外,检查 num > 1
是愚蠢的,因为您从 100 开始范围。而且您不必将 digits
转换为列表;它已经是一个列表。
您可以通过在 get_primes
中使用 any
函数并在 filter_primes
中使用 yield
来缩短它的长度。稍后你会得到它。
def get_primes():
numere_prime = []
for num in range(100, 1000):
prime = True
for i in range(2, 34):
if (num % i) == 0:
prime = False
break
if prime:
numere_prime.append(num)
return numere_prime
def filter_primes(primes, magic):
res = []
for elem in primes:
digits = [int(x) for x in str(elem)]
product = 1
for n in digits:
product *= n
if product == magic:
res.append(elem)
return res
numere_prime = get_primes()
inp = int(input("Your value: "))
result = filter_primes(numere_prime, inp)
print(result)
primes = [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163]
a = int(input('enter a nmber :'))
primes_useful = []
for i in primes :
product = 1
for k in str(i):
product *= int(k)
if product == a :
primes_useful.append(i)
print(primes_useful)
numere_prime = []
for num in range(100, 1000):
prime = True
for i in range(2, num):
if (num % i) == 0:
prime = False
if prime:
numere_prime.append(num)
def prime_digit_prod(x):
output = []
for elem in numere_prime:
digits = [int(x) for x in str(elem)] # split digits into [1,0,1][1,0,3]...
result = 1
for n in digits:
result *= n
if result == x:
output.append(elem)
return output
print(prime_digit_prod(9))
输出:
[191, 313, 331, 911]
numere_prime = [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157]
digi_mult_dict = {}
for elem in numere_prime:
c = elem % 10
b = int((elem % 100 - c)/10)
a = int((elem - b - c)/100)
mult = a * b * c
digi_mult_dict[elem] = mult
numero = input("input a number")
for key, value in digi_mult_dict.items():
if int(value) == int(numero):
print(key)
我有 3 位质数数组:
for num in range(100, 1000):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
numere_prime.append(num)
列表是:
numere_prime = [[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163...]
我需要的是将一个数字的每个数字相乘(例如 101 = 1 * 0 * 1),然后它显示所有 numere_prime 相乘后等于用户输入。 到目前为止我做了什么:
for elem in numere_prime:
digits = [int(x) for x in str(elem)] # split digits into [1,0,1][1,0,3]...
for n in list(digits):
// n * n_at_next_index * n_at_next_index
if //result of multiplying == //number set by input:
//append digits to result list
结果应该是这样的:input (9) = [191, 313, 331, 911]
你的素数算法是错误的。您不能仅在任何因素失败时追加到列表中,您必须知道所有因素都失败了。而且您不必一直搜索到 num
;您只需向上搜索 sqrt(num)
。由于最大值是 1000,您可以在 33 处停止。此外,检查 num > 1
是愚蠢的,因为您从 100 开始范围。而且您不必将 digits
转换为列表;它已经是一个列表。
您可以通过在 get_primes
中使用 any
函数并在 filter_primes
中使用 yield
来缩短它的长度。稍后你会得到它。
def get_primes():
numere_prime = []
for num in range(100, 1000):
prime = True
for i in range(2, 34):
if (num % i) == 0:
prime = False
break
if prime:
numere_prime.append(num)
return numere_prime
def filter_primes(primes, magic):
res = []
for elem in primes:
digits = [int(x) for x in str(elem)]
product = 1
for n in digits:
product *= n
if product == magic:
res.append(elem)
return res
numere_prime = get_primes()
inp = int(input("Your value: "))
result = filter_primes(numere_prime, inp)
print(result)
primes = [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163]
a = int(input('enter a nmber :'))
primes_useful = []
for i in primes :
product = 1
for k in str(i):
product *= int(k)
if product == a :
primes_useful.append(i)
print(primes_useful)
numere_prime = []
for num in range(100, 1000):
prime = True
for i in range(2, num):
if (num % i) == 0:
prime = False
if prime:
numere_prime.append(num)
def prime_digit_prod(x):
output = []
for elem in numere_prime:
digits = [int(x) for x in str(elem)] # split digits into [1,0,1][1,0,3]...
result = 1
for n in digits:
result *= n
if result == x:
output.append(elem)
return output
print(prime_digit_prod(9))
输出:
[191, 313, 331, 911]
numere_prime = [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157]
digi_mult_dict = {}
for elem in numere_prime:
c = elem % 10
b = int((elem % 100 - c)/10)
a = int((elem - b - c)/100)
mult = a * b * c
digi_mult_dict[elem] = mult
numero = input("input a number")
for key, value in digi_mult_dict.items():
if int(value) == int(numero):
print(key)