八.9
对任意给定的一个素数 p,求出 Zp* 的最小生成元:
int main()
{
int p; int i = 2;
int aaa[100];
int k = 0;
cin >> p;
int p1 = p; int x = p - 1;
while (i <= x)
{
if (x % i == 0)
{
x = x / i;
aaa[k] = i;
k++;
}
else i++;
}
int temp = 0;
int Zp[100]; int count = 0;
for (int a = 2; a < p1; a++)
{
int record = 1;
for (int i = 0; i < k; i++)
{
temp = pow(a,((p1-1)/aaa[i]));
if ((temp % p1) == 1)
{
record = 0;
}
}
if (record == 1) { Zp[count] = a; count++; }
}
cout << Zp[0];
return 0;
}
任取一个整数 n,大于 1 小于 n 的所有素数 p,求 Zp* 的最小生成元,并求以上最小生成元集合中最大者所对应的素数 p:
int main()
{
int aaa[100];
int n;
cout << “请输入n:”;
cin >> n;
int Z[100]; Z[0] = 2;int y = 1;
for (int i = 3; i < n; i++)
{
int x = 1;
for (int j = 2; j < sqrt(n); j++)
{
if (i % j == 0)
{
x = 0;
}
}
if (x == 1) { Z[y] = i; y++; }
}
int max = 0; int d;
for (int b = 0; b < y; b++)
{
int temp1 = Z[b]-1; int k = 0; int i = 2;
while(i<= temp1)
{
if (temp1 % i == 0)
{
temp1 = temp1 / i;
aaa[k] = i;//将素因子传入数组
k++;
}
else i++;
}
int temp = 0;
int Zp[100] = {0}; int count = 0;
for (int a = 2; a <= Z[b]; a++)
{
int record = 1;
for (int l = 0; l < k; l++)
{
temp = pow(a, ((Z[b] - 1) / aaa[l]));
if ((temp % Z[b]) == 1)
{
record = 0;
}
}
if (record == 1) { Zp[count] = a; count++; }
}
cout << Zp[0]<<" ";
if (Zp[0] > max)
{ max = Zp[0]; d = Z[b]; }
}
cout << endl;
cout << max << " " << d;
return 0;
}