Answer to Question #318866 in Python for Ricks

Question #318866

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-30T02:50:47-0400

Use the following set of codes:

# A program that accepts non-negative integers and prints common divisors
numbers = []
n = int(input("How many numbers? "))
for i in range(1, n + 1):
    num = int(input("\nInput number " + str(i) + ": "))
    numbers.append(num)
    print("Divisors of", num, "are:")
    for j in range(1, num + 1):
        if num % j == 0:
            print(j, end=' ')

def gcd(a, b):
    if a == 0:
        return b
    return gcd(b % a, a)


# Function to print all the common divisors
def print_divisors(arr, N):
    # Variable to find the gcd of N numbers
    g = arr[0]

    # Set to store all the common divisors
    divisors = dict()

    # Finding GCD of the given N numbers
    for k in range(1, N):
        g = gcd(arr[k], g)

    # Finding divisors of the HCF of n numbers
    for k in range(1, g + 1):
        if i * k > g:
            break
        if g % k == 0:
            divisors[k] = 1
            if g // k != k:
                divisors[g // k] = 1

    # Print all the divisors
    print("\nCommon divisor is/are:")
    for number in sorted(divisors):
        print(number, end=" ")

if __name__ == '__main__':
    arr = numbers
    n = len(arr)
    print_divisors(arr, n)

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