lab12(fermat)

 #include <iostream> 

#include <cmath> 

using namespace std; 


long long modExp(long long base, long long exp, long long mod) { 

    long long result = 1; 

    base = base % mod; 

    while (exp > 0) { 

        if (exp % 2 == 1) 

            result = (result * base) % mod; 

        base = (base * base) % mod; 

        exp /= 2; 

    } 

    return result; 


void fermatTheoremDemo() { 

    long long a, p; 

    cout << "Enter a prime number (p): "; 

    cin >> p; 

    cout << "Enter an integer a (a should not be divisible by p): "; 

    cin >> a; 

    if (a % p == 0) { 

        cout << "Error: 'a' should not be divisible by 'p'." << endl; 

        return; 

    } 

    long long result = modExp(a, p - 1, p); 

    cout << a << "^(" << p - 1 << ") mod " << p << " = " << result << endl; 

    if (result == 1) { 

        cout << "Fermat's Little Theorem is verified!" << endl; 

    } else { 

        cout << "Fermat's theorem does not hold. Check input values." << endl; 

    } 


int main() { 

    fermatTheoremDemo(); 

    return 0; 

}


Comments

Popular posts from this blog

lab7

lab12(euler)

lab6b