利用二次互反律,写程序完成勒让德符号 ( q p ) \left(\frac{q}{p}\right) (pq)的计算, p p p和 q q q是任意的奇素数。
#include<iostream>
#include<cmath>
using namespace std;
bool IsSqrt(int n) {
for (int i = 2; i <= sqrt(n); i++) {
if (i*i == n)return 1;
}return 0;
}
int Judgement(int p) {
if (p % 4 == 1)return 1;
else if (p % 4 == -1)return -1;
}
int Judgement1(int p) {
if (p % 8 == 1 || p % 8 == 7)return 1;
else if (p % 8 == 3 || p % 8 == 5)return -1;
}
int Function(int q, int p) {
if (IsSqrt(q))return 1;
else if (q == -1)return Judgement(p);
else if (q%2==0)return Judgement1(p)*Function(q/2,p);
else return Function(p%q, q)*(Judgement(p)&Judgement1(q));
}