text: Modular multiplicative inverse from 1 to n | A naive method to find modular multiplicative inverse of ' a ' under modulo ' prime ' ; Driver code
code: function modInverse ( a , prime ) { a = a % prime ; for ( let x = 1 ; x < prime ; x ++ ) if ( ( a * x ) % prime == 1 ) return x ; return - 1 ; } function printModIverses ( n , prime ) { for ( let i = 1 ; i <= n ; i ++ ) document . write ( modInverse ( i , prime ) + " " ) ; } let n = 10 ; let prime = 17 ; printModIverses ( n , prime ) ;