C,n = 3030
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define R return
#define u32 unsigned
#define F for
#define P printf
int isq(u32 a)
{u32 y,x,t,i;
static u32 arr720[]={0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484,529,576,625,676,180,241,304,369,436,505,649,160,409,496,585,340,544,145,601,244,580,481,640,385,265};
static char barr[724]={0};
if(barr[0]==0)F(i=0;i<(sizeof arr720)/sizeof(unsigned);++i)
if(arr720[i]<720) barr[arr720[i]]=1;
if(barr[a%720]==0) R 0;
y=sqrt(a);
R y*y==a;
}
int f(u32 a, u32 b, u32 c)
{u32 t,x;
if(c&1)R 0;
t= a*a+b*b;
if(t&1)R 0;
R isq((2*t-c*c)/4);
}
int h(u32 n)
{u32 cnt,a,c,k,ke,kc,d,v,l,aa,bb,cc;
cnt=0;
F(a=1;a<=n;++a)
{ke=(n-a)/2;
F(k=0;k<=ke;++k)
{v=a+k;
d=v*v+k*k;
l=sqrt(d);
v=n/2;
if(l>v)l=v;
v=a+k-1;
if(l>v)l=v;
F(c=k+1;c<=l;++c)
{if(isq(d-c*c))
{bb=a+2*k;cc=2*c;
if(bb>cc && f(a, cc,bb)) continue;
if( a>cc && f(cc,bb, a)) continue;
++cnt;
//P("|a=%u b=%u c=%u", a, bb, cc);
}
}
}
}
R cnt;
}
int main(int c, char** a)
{time_t ti, tf;
double d;
int ni;
u32 n,i;
if(c!=2||a[1]==0){P("uso: questo_programma.exe arg1\n ove arg1 e\' un numero positivo\n");R 0;}
ni=atoi(a[1]);
if(ni<=0){P("Parametro negativo o zero non permesso\n");R 0;}
n=ni;
if(n>0xFFFFF){P("Parametro troppo grande non permesso\n"); R 0;}
F(i=3;i<33;++i)if(i<10||i>21)P("T(%u)=%u|",i, h(i));
ti=time(0);
P("\nT(%u)=%u\n", n, h(n));
tf=time(0);
d=difftime(tf,ti);
P("Tempo trascorso = %.2f sec\n", d);
R 1;
}
结果:
C:\Users\a\b>prog 3030
T(3)=0|T(4)=0|T(5)=0|T(6)=1|T(7)=1|T(8)=2|T(9)=3|T(22)=34|T(23)=37|T(24)=42|T(25)=
45|T(26)=56|T(27)=59|T(28)=65|T(29)=67|T(30)=74|T(31)=79|T(32)=91|
T(3030)=3321226
Tempo trascorso = 60.00 sec
上面的代码将是公理答案的C语言转换(如果我们不计算isq()函数的话)。
我的编译器未链接其他人使用sqrtf()的函数...这里没有float的sqrt函数...他们确定sqrtf是C标准函数吗?