Given two numbers a and b find all x such that a % x = b | Javascript program to find x such that a % x is equal to b . ; If a is less than b then no solution ; If a is equal to b then every number greater than a will be the solution so its infinity ; Count variable store the number of values possible ; checking for both divisor and quotient whether they divide ( a - b ) completely and greater than b . ; Here y is added twice in the last iteration so 1 y should be decremented to get correct solution ; Driver Code
function modularEquation ( a , b ) { if ( a < b ) { document . write ( " " ) ; return ; } if ( a == b ) { document . write ( " " ) ; return ; } let count = 0 ; let n = a - b ; let y = Math . sqrt ( a - b ) ; for ( let i = 1 ; i <= y ; ++ i ) { if ( n % i == 0 ) { if ( n / i > b ) count ++ ; if ( i > b ) count ++ ; } } if ( y * y == n && y > b ) count -- ; document . write ( count ) ; } let a = 21 ; let b = 5 ; modularEquation ( a , b ) ;