How do I print out a list of common factors for two numbers?

心不动则不痛 提交于 2021-01-27 21:14:05

问题


I need to print out a list of common factors for two numbers

def print_nums(x, y):
    for i in range(1, x + 1):
        if x % i == 0:
            print(i)
    for t in range(1, y + 1):
        if y % t == 0:
            print(t)


number = int(input("Enter a number: "))
number2 = int(input("Enter a second number: "))

print("Common factors are: ".format(number, number2))
print_nums(number, number2)

It prints out both lists but not the common factors of each


回答1:


def print_nums(x, y):
    zet = []
    for i in range(1, x + 1):
        if x % i == 0:
            #print(i)
            zet.append(i)
    for t in range(1, y + 1):
        if y % t == 0 and t in zet:
            print(t)


number = int(input("Enter a number: "))
number2 = int(input("Enter a second number: "))

print("Common factors are:")
print_nums(number, number2)

Example of running the code:

Enter a number: 24                                                                                                                                                                  
Enter a second number: 18                                                                                                                                                           
Common factors are:                                                                                                                                                                 
1                                                                                                                                                                                   
2                                                                                                                                                                                   
3                                                                                                                                                                                   
6  



回答2:


Your code just prints factors, not common factors. You could loop over x and y's common range and check if i is a factor of both:

def common_factors(x, y):
    for i in range(2, min(x, y)+1):  # 1 is trivial, so ignore it
        if x % i == 0 and y % i == 0:  # If x and y are both multiples of i
            yield i

print(list(common_factors(9, 12)))  # -> [3]



回答3:


basically same thought as the above solutions..but a nifty function to basically check for only items in the list which are non-unique..like the opposite of set...this returns common factors because they will occur more than once

from iteration_utilities import duplicates, unique_everseen

facs=[]

def print_nums(x, y, facs):
    for i in range(1, x + 1):
        if x % i == 0:
            facs.append(i)
    for t in range(1, y + 1):
        if y % t == 0:
            facs.append(t)
    return list(unique_everseen(duplicates(facs)))


来源:https://stackoverflow.com/questions/57780560/how-do-i-print-out-a-list-of-common-factors-for-two-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!