
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Euler Theorem
Euler's theorem states that if two numbers (a and n), are co-prime i.e. gcd(a, n)=1, then 'a' raised to the power of Euler's totient function of n (a^φ(n)) is congruent to 1 modulo n i.e. a^φ(n) ≡ 1 (mod n). The Euler's Totient Function is denoted as φ(n) and represents the count of integers from 1 to n that are relatively co-prime to n.
In this article, we have two co-prime integers. Our task is to implement the Euler Theorem in C++ using given co-prime numbers. Here is an example to verify the Euler Theorem for two co-prime numbers:
Input: a = 5, n = 21 Output: Euler's Theorem: 5^12 ≡ 1 (mod 21)
Here is the explanation of the above example:
a = 5, n = 21, gcd(5, 21) = 1 φ(21) = 21(1-1/3)(1-1/7) = 12 [Using Totient Function Formula] 5^12 mod 21 = 1 a^φ(n) ≡ 1 (mod n) => 5^12 ≡ 1 (mod 21)
Steps to Implement Euler Theorem
The steps to implement Euler's theorem are as follows:
- The gcd() function accepts two integers (a and n) as arguments and calculates the gcd of both integers.
- Then we have defined a phi() function that calculates Euler's Totient function φ(n) to count the number of integers that are co-prime with n. The outer for loop finds the prime factors of n.
- The inner while loop removes all the prime factors of the n. The result variable gives the number of integers that are co-prime with n by removing the prime factors of n.
- The powerMod() function computes (base^exp) % mod.
- In the main() function, we have used the if statement to check if both the integers are co-prime using the gcd() function. If not co-prime, then returns the message "Euler theorem not applicable".
C++ Program to Implement Euler Theorem
The following code implements Euler theorem in C++ using the steps mentioned above:
#include <iostream> using namespace std; // Function to compute GCD int gcd(int a, int b) { while (b != 0) { int rem = a % b; a = b; b = rem; } return a; } // Function to compute Euler's Totient Function phi-n int phi(int n) { int result = n; for (int 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; } // Fast modular exponentiation: (base^exp) % mod int powerMod(int base, int exp, int mod) { int result = 1; base %= mod; while (exp > 0) { if (exp % 2 == 1) result = (1LL * result * base) % mod; base = (1LL * base * base) % mod; exp /= 2; } return result; } int main() { int a = 5; int n = 21; if (gcd(a, n) != 1) { cout << "Euler's Theorem not applicable: a and n must be co-prime." << endl; return 0; } int phi_n = phi(n); int result = powerMod(a, phi_n, n); cout << "a = " << a << ", n = " << n << ", phi-n = " << phi_n << endl; cout << "Euler's Theorem: " << a << "^" << phi_n << " ? " << result << " (mod " << n << ")" << endl; return 0; }
The output of the above code is:
a = 5, n = 21, phi-n = 12 Euler's Theorem: 5^12 ≡ 1 (mod 21)