Common divisors of N numbers | Function to calculate gcd of two numbers ; Function to print all the common divisors ; Variable to find the gcd of N numbers ; Set to store all the common divisors ; Finding GCD of the given N numbers ; Finding divisors of the HCF of n numbers ; Print all the divisors ; Driver 's Code ; Function to print all the common divisors
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function printAllDivisors ( arr , N ) { let g = arr [ 0 ] ; let divisors = new Set ( ) ; for ( let i = 1 ; i < N ; i ++ ) { g = gcd ( arr [ i ] , g ) ; } for ( let i = 1 ; i * i <= g ; i ++ ) { if ( g % i == 0 ) { divisors . add ( i ) ; if ( Math . floor ( g / i ) != i ) divisors . add ( Math . floor ( g / i ) ) ; } } for ( let it of divisors . values ( ) ) document . write ( it + " " ) ; } let arr = [ 6 , 90 , 12 , 18 , 30 , 24 ] ; let n = arr . length ; printAllDivisors ( arr , n ) ;