lab12(euler)

 #include <iostream> 

#include <cmath> 

using namespace std; 


long long gcd(long long a, long long b) { 

    if (b == 0) 

        return a; 

    return gcd(b, a % b); 


long long eulerTotient(long long n) { 

    long long result = n; 

    for (long long i = 2; i * i <= n; i++) { 

        if (n % i == 0) { 

            while (n % i == 0) 

                n /= i; 

            result -= result / i; 

        } 

    } 

    if (n > 1) 

        result -= result / n; 

    return result; 


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 eulerTheoremDemo() { 

    long long a, n; 

    cout << "Enter a positive integer (n): "; 

    cin >> n; 

    cout << "Enter an integer (a) such that gcd(a, n) = 1: "; 

    cin >> a; 

    if (gcd(a, n) != 1) { 

        cout << "Error: 'a' must be coprime to 'n'." << endl; 

        return; 

    } 

    long long phi_n = eulerTotient(n); 

    cout << "Euler's Totient Function φ(" << n << ") = " << phi_n << endl; 

    long long result = modExp(a, phi_n, n); 

    cout << a << "^" << phi_n << " mod " << n << " = " << result << endl; 

    if (result == 1) { 

        cout << "Euler's Theorem is verified!" << endl; 

    } else { 

        cout << "Euler's Theorem failed. Check input values." << endl; 

    } 


int main() { 

    eulerTheoremDemo(); 

    return 0; 

}


Comments

Popular posts from this blog

lab7

lab6b