Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Python3 program for the above approach ; 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 ; Driver Code ; Given N and X ; Function Call
from math import sqrt NEW_LINE def countOccurrences ( N , X ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , int ( sqrt ( X ) ) + 1 ) : NEW_LINE INDENT if X % i == 0 : NEW_LINE INDENT a = i NEW_LINE b = X // i NEW_LINE if a <= N and b <= N : NEW_LINE INDENT if a == b : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 2 NEW_LINE DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE X = 12 NEW_LINE print ( countOccurrences ( N , X ) ) NEW_LINE DEDENT