[英]want to find number with the most prime divisors but it keeps printing the last inputed number and its divisors
[英]number with the most prime divisors
我已經完成了一個程序,它可以正常工作,但我覺得它可能會更好,更先進,所以我想要一些關於我的程序的主要建議,它將接受許多數字並返回具有最多素數除數的數字和它前面的除數數像這樣的數字678 3
作為 output
代碼:
list_of_inputs = []
for elements in range(5):
list_of_inputs.append(int(input()))
list_of_inputs = list(set(list_of_inputs))
def _check_prime_sample_(n):
if n < 2:
return False
# Iterate from 2 to the number
for i in range(2, n):
if n % i == 0:
return False
return True
def find_integer_with_most_divisors(input_list):
from operator import itemgetter
lis = []
dic = {}
for elements in input_list:
sub_lis = []
for items in range(1, elements):
if elements % items == 0 and _check_prime_sample_(items) == True:
sub_lis.append(items)
lis.append(len(sub_lis))
dic[elements] = len(sub_lis)
tup = [(k, v) for k, v in dic.items()]
sup = max(tup, key=itemgetter(1, 0))
print('{} {}'.format((sup)[0], (sup)[1]))
find_integer_with_most_divisors(list_of_inputs)
您應該編寫一個 function 來返回素因子的數量並使用 max() function 來獲得結果:
def primeFactors(N):
count = 0
p = 2 # prime candidate
while p*p<=N: # find primes up to √N
n = N
while N%p == 0: N //= p # remove prime factor from N
count += (n!=N) # count factors
p += 1 + p%2 # next prime candidate
return count + (N>1)
output:
list_of_inputs = list(range(100))
count,number = max( (primeFactors(n),n) for n in set(list_of_inputs) )
print(number,count) # 90 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.