Number of solutions to Modular Equations | Returns the number of divisors of ( A - B ) greater than B ; If N is divisible by i ; Count only the divisors greater than B ; Checking if a divisor isnt counted twice ; Utility function to calculate number of all possible values of X for which the modular equation holds true ; If A = B there are infinitely many solutions to equation or we say X can take infinitely many values > A . We return - 1 in this case ; If A < B , there are no possible values of X satisfying the equation ; The last case is when A > B , here we calculate the number of divisors of ( A - B ) , which are greater than B ; Wrapper function for numberOfPossibleWaysUtil ( ) ; If infinitely many solutions available ; Driver code
function calculateDivisors ( A , B ) { let N = ( A - B ) ; let noOfDivisors = 0 ; for ( let i = 1 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( ( N % i ) == 0 ) { if ( i > B ) noOfDivisors ++ ; if ( ( N / i ) != i && ( N / i ) > B ) noOfDivisors ++ ; } } return noOfDivisors ; } function numberOfPossibleWaysUtil ( A , B ) { if ( A == B ) return - 1 ; if ( A < B ) return 0 ; let noOfDivisors = 0 ; noOfDivisors = calculateDivisors ( A , B ) ; return noOfDivisors ; } function numberOfPossibleWays ( A , B ) { let noOfSolutions = numberOfPossibleWaysUtil ( A , B ) ; if ( noOfSolutions == - 1 ) { document . write ( " " + A + " " + B + " " + " " + " " + A + " " ) ; } else { document . write ( " " + A + " " + B + " " + noOfSolutions + " " + " " ) ; } } let A = 26 , B = 2 ; numberOfPossibleWays ( A , B ) ; A = 21 , B = 5 ; numberOfPossibleWays ( A , B ) ;