Count of pairs in given range having their ratio equal to ratio of product of their digits | Function to find the product of digits of the given number ; Function to find the count of pairs ( a , b ) such that a : b = ( product of digits of a ) : ( product of digits of b ) ; Stores the count of the valid pairs ; Loop to iterate over all unordered pairs ( a , b ) ; Stores the product of digits of a ; Stores the product of digits of b ; If x != 0 and y != 0 and a : b is equivalent to x : y ; Increment valid pair count ; Return Answer ; Driver code ; Function Call
function getProduct ( n ) { let product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = Math . floor ( n / 10 ) ; } return product ; } function countPairs ( L , R ) { let cntPair = 0 ; for ( let a = L ; a <= R ; a ++ ) { for ( let b = a + 1 ; b <= R ; b ++ ) { let x = getProduct ( a ) ; let y = getProduct ( b ) ; if ( x && y && ( a * y ) == ( b * x ) ) { cntPair ++ ; } } } return cntPair ; } let L = 1 ; let R = 100 ; document . write ( countPairs ( 1 , 100 ) ) ;