text: Modular multiplicative inverse from 1 to n | C ++ program to find modular inverse of all numbers from 1 to n using naive method ; A naive method to find modular multiplicative inverse of ' a ' under modulo ' prime ' ; Driver Program
code: #include <iostream> NEW_LINE using namespace std ; int modInverse ( int a , int prime ) { a = a % prime ; for ( int x = 1 ; x < prime ; x ++ ) if ( ( a * x ) % prime == 1 ) return x ; return -1 ; } void printModIverses ( int n , int prime ) { for ( int i = 1 ; i <= n ; i ++ ) cout << modInverse ( i , prime ) << " ▁ " ; } int main ( ) { int n = 10 , prime = 17 ; printModIverses ( n , prime ) ; return 0 ; }