페르마의 작은 정리는 기본 정수론의 기본 결과 중 하나이며 페르마 소수성 검정의 기초입니다. 정리는 1640년에 피에르 드 페르마의 이름을 따서 명명되었습니다. 정리에 따르면 p가 소수이면 임의의 정수 a에 대해 숫자 a p-a는 p의 정수 배수입니다.
알고리즘
Begin Function power() is used to compute a raised to power b under modulo M function modInverse() to find modular inverse of a under modulo m : Let m is prime If a and m are relatively prime, then modulo inverse is a^(m - 2) mod m End
예시 코드
#include <iostream>
using namespace std;
int pow(int a, int b, int M) {
int x = 1, y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y);
if (x > M)
x %= M;
}
y = (y * y);
if (y > M)
y %= M;
b /= 2;
}
return x;
}
int modInverse(int a, int m) {
return pow(a, m - 2, m);
}
int main() {
int a, m;
cout<<"Enter number to find modular multiplicative inverse: ";
cin>>a;
cout<<"Enter Modular Value: ";
cin>>m;
cout<<modInverse(a, m)<<endl;
} 출력
Enter number to find modular multiplicative inverse: 26 Enter Modular Value: 7 3