Saturday, June 10, 2017

Count of common factors

Given a set of numbers, the program must find the count of the common factors C excluding 1. 

Input Format: 

First line will contain the integer value N representing how many numbers are passed as input. Next N lines will have the numbers. 
Output Format: 
First line will contain the count of common factors C.

Sample Input/Output: 

Example 1: 
Input: 
100 
75 

Output: 

Explanation: The common factors excluding 1 are 5,25. Hence output is 2 

Example 2: 
Input: 
10 
20 
30 

Output: 
3

Explanation: The common factors excluding 1 are 2,5,10. Hence output is 3

Solution:

from fractions import gcd
from math import sqrt
import functools
n = []
for _ in range(int(input())):
    n.append(int(input()))

g = functools.reduce(gcd,n)
c = 0
for i in range(1,int(sqrt(g))+1):
    if g%i == 0:
        if g/i == i:
            c += 1
        else:
            c += 2
print(c-1)
Share: