Saturday 18 November 2017

C++ Program to Implement Fermat’s Little Theorem


Code:

#include   iostream
using namespace std;

/* calculates (a^b)%MOD */
int pow(int a, int b, int MOD) 
{
    int x = 1, y = a;
    while (b > 0) 
    {
        if (b % 2 == 1) 
        {
            x = (x * y);
            if (x > MOD) 
                x %= MOD;
        }
        y = (y * y);
        if (y > MOD) 
            y %= MOD;
        b /= 2;
    }
    return x;
}

int modInverse(int a, int m) 
{
    return pow(a, m - 2, m);
}
//Main
int main()
{
    int a, m;
    cout<<"Enter number to find modular multiplicative inverse: ";
    cin>>a;
    cout<<"Enter Modular Value: ";
    cin>>m;
    cout<
}


Output:

Enter number to find modular multiplicative inverse: 1111
Enter Modular Value: 331
216

------------------
(program exited with code: 1)
Press return to continue


More C++ Programs:













100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...