Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Function to count the occurrences of X in the generated square matrix ; Stores the required result ; Iterate upto square root of X ; Check if i divides X ; Store the quotient obtained on dividing X by i ; If both the numbers fall in the range , update count ; Return the result ; Given N and X ; Function Call
function countOccurrences ( N , X ) { var count = 0 ; for ( var i = 1 ; i < Math . sqrt ( X ) ; i ++ ) { if ( X % i == 0 ) { var a = i ; var b = X / i ; if ( a <= N && b <= N ) { if ( a == b ) count += 1 ; else count += 2 ; } } } return count ; } var N = 7 ; var X = 12 ; document . write ( countOccurrences ( N , X ) ) ;