哎,不够严谨啊。
程序代码:
/**
* (c + b)(c - b) = a * a
* 令 a * a = x * y,解得:
* b = (y - x) / 2, c = (y + x) / 2
**/
#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <stdio.h>
#define MAX 100
struct MyStruct
{
int N;
struct Struct
{
int n, count;
}data[6];
}sa;
int count = 4, prime[9592] = {2, 3, 5, 7};
/*
* 初始化质数表
**/
void Init()
{
int n, i, flag;
for (n = 10;n < MAX;n += 1)
{
flag = 1;
for (i = 0;prime[i] * prime[i] <= n;i++)
{
if (0 == n % prime[i])
{
flag = 0;
break;
}
}
if (flag)
{
prime[count++] = n;
}
}
}
/*
* 将 n 分解质因数,存入结构体 sa
**/
void InitSA(int n)
{
int i = 0;
sa.N = 0;
while (1 != n)
{
while (0 != n % prime[i]) i += 1;
sa.data[sa.N].n = prime[i];
sa.data[sa.N].count = 0;
while (0 == n % prime[i])
{
n /= prime[i];
sa.data[sa.N].count += 1;
}
sa.N += 1;
}
}
/*
* a * a = x * y
**/
void Show1(int step, int x, int a)
{
int i, y, b, c;
if (step == sa.N)
{
y = a * a / x;
b = (y - x) / 2;
c = (y + x) / 2;
if (c <= MAX && b > a)
{
printf("%6d, %6d, %6d\n", a, b, c);
}
return;
}
for (i = 0;i <= sa.data[step].count * 2;i += 1)
{
if (i) x *= sa.data[step].n;
if (x >= a) { return;}
Show1(step + 1, x, a);
}
}
/*
* a * a = 2 ^ p = (2 ^ n) * (2 ^ (p - n))
**/
void Show2(int p, int a)
{
int i, x = 1, y, b, c;
for (i = 1;i < p;i += 1)
{
x *= 2;
y = a * a / x;
b = (y - x) / 2;
c = (y + x) / 2;
if (c <= MAX && b > a)
{
printf("%6d, %6d, %6d\n", a, b, c);
}
}
}
/*
* a * a = 2 ^ p * M = (2 ^ n * m) * (2 ^ (p - n) * m)
**/
void Show3(int a, int p, int m)
{
int i, b, c, y, x = m;
for (i = 1;i < p;i += 1)
{
x *= 2;
y = a * a / x;
b = (y - x) / 2;
c = (y + x) / 2;
if (c <= MAX && b > a)
{
printf("%6d, %6d, %6d\n", a, b, c);
}
}
}
/*
* a * a = 2 ^ p * M = (2 ^ n * x) * (2 ^ (p - n) * y) (x != y)
**/
void Show4(int step, int x, int n, int p, int a)
{
int i, b, c, y;
if (step == sa.N)
{
for (i = 1;i < p * 2;i += 1)
{
x *= 2;
y = a * a / x;
b = abs(y - x) / 2;
c = (y + x) / 2;
if (c <= MAX && b > a)
{
printf("%6d, %6d, %6d\n", a, b, c);
}
}
return;
}
for (i = 0;i <= sa.data[step].count * 2;i += 1)
{
if (i) x *= sa.data[step].n;
if (x >= n) { return; }
Show4(step + 1, x, n, p, a);
}
}
int main(int argc, char* argv[])
{
int a, p, M;
Init();
freopen("out.txt", "w", stdout);
for (a = 3;a <= MAX;a += 1)
{
if (0 != a % 2)
{
InitSA(a);
Show1(0, 1, a);
continue;
}
M = a, p = 0;
while (0 == M % 2)
{
p += 1;
M /= 2;
}
if (1 == M)
{
Show2(p, a);
}
else
{
InitSA(M);
Show3(a, p, M);
Show4(0, 1, M, p, a);
}
}
fclose(stdout);
return 0;
}