Count of integers that divide all the elements of the given array | Javascript implementation of the approach ; Function to return the count of the required integers ; To store the gcd of the array elements ; To store the count of factors of the found gcd ; If g is a perfect square ; Factors appear in pairs ; Driver code
function calgcd ( a , b ) { if ( b == 0 ) return a ; return calgcd ( b , a % b ) ; } function getCount ( a , n ) { let gcd = 0 ; for ( let i = 0 ; i < n ; i ++ ) gcd = calgcd ( gcd , a [ i ] ) ; let cnt = 0 ; for ( let i = 1 ; i * i <= gcd ; i ++ ) { if ( gcd % i == 0 ) { if ( i * i == gcd ) cnt ++ ; else cnt += 2 ; } } return cnt ; } let a = [ 4 , 16 , 1024 , 48 ] ; let n = a . length ; document . write ( getCount ( a , n ) ) ;