在n=3,4,5的情况下验证费马大定理在0-1000范围内的整数成立
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#define N 1000using namespace std;unsigned long long Pow(int x, int n)//高效率求x^n 可以在log(n)的复杂度下完成{long long base = x, ans = 1;while (n > 1){if (n & 1)//n % 2 == 1ans *= base;base *= base;n >>= 1;//n /= 2}ans *= base;return ans;}int main(){int n;int i, j, k;short plan, lastPlan = -1;char bar[102];char lab[] = "\\|//";for (n = 3; n < 6; n++){memset(bar, 0, 102);lastPlan = -1;for (i = 1; i < N; i++){for (j = i; j < N; j++){for (k = i + 1; k < N; k++){if (fabs(pow(i, n) + pow(j, n) - pow(k, n)) < 1e-6)//if(Pow(i,n)+Pow(j,n)==Pow(k,n)){cout << i << " " << j << " " << k << endl;cout << "Error!\n" << endl;return 0;}}}plan = i * 100 / N;if (plan > lastPlan){bar[plan] = '#';printf("n = %d [%-101s] [%d%%] [%c]\r", n, bar, plan, lab[plan % 4]);lastPlan = plan;}}bar[100] = '#';printf("n = %d,[%-101s] [%d%%] [%c]\n", n, bar, 100, lab[100 % 4]);}cout << "Succeed!" << endl;return 0;}