Answer to Question #318611 in Python for Tine

Question #318611

Develop a Python application which will accept n non-negative integers and will display the DIVISORS of each number then will determine the COMMON DIVISORS of the input integers.



Sample Output:



How many numbers? 4



Input Number 1: 10



Input Number 2: 50



Input Number 3: 15



Input Number 4: 20



Divisors



10: 1 2 5 10



50: 1 2 5 25 10 50



15: 1 3 5 15



20: 1 2 5 10 20



COMMON Divisors is/are: 15

1
Expert's answer
2022-03-26T08:42:33-0400
def get_divisors(x):
    ans = {1, x}
    for i in range(2, int(x**0.5)+1):
        if x%i == 0:
            ans.add(i)
            ans.add(x//i)
    return ans


n = int(input("How many numbers? "))
arr = []
for i in range(n):
    m = int(input(f"Input Number {i+1}: "))
    arr.append(m)

print("Divisors")
com_div = {}
for x in arr:
    divs = get_divisors(x)
    print(str(x) + ": " + " ".join([str(div) for div in sorted(divs)]))
    if len(com_div) == 0:
        com_div = divs
    else:
        com_div = com_div&divs

print("COMMON Divisors is/are: " + " ".join([str(div) for div in sorted(com_div)]))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS