/*CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC C C C COMPUTE MERTENS FUNCTION (sum of M(x/i)^2) C C 12/20/14 (DKC) C C C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC*/ #include <math.h> #include <stdio.h> unsigned int max=1000; // maximum x value unsigned int start=2; // beginning x value, normally set to 2 unsigned int out=1; // output flag, 0 for M(x), 1 for sum, 2 for histogram unsigned int bins=100; // number of histogram bins (for out=2) unsigned int n=1; // decimation factor (normally set to 1) unsigned int sflag=0; // square root flag unsigned int limit=0; // partial sum flag (usually set to 0) void main() { short m[500000]; int sum,t; unsigned int i,j,index,hisdif[101],width,flag,savsum,savind,numone; double delta,newsum,oldsum,maxdel,maxsav[10]; FILE *Outfp; Outfp = fopen("out7.dat","w"); if (max>500000) { printf("x value too large \n"); goto zskip; } if ((out==2)&&(sflag==0)) { printf("parameter error \n"); goto zskip; } // // compute Mertens function // m[0]=1; if (out==0) { // printf(" %d \n",1); fprintf(Outfp," %d \n",1); } for (index=2; index<=max; index++) { sum=0; for (i=2; i<=(index/3); i++) sum=sum+m[index/i-1]; sum=sum+(index+1)/2; t=1-sum; if ((t>32767)||(t<-32768)) { printf("overflow \n"); goto zskip; } m[index-1]=(short)t; if (out==0) { // printf(" %d \n",1-sum); fprintf(Outfp," %d \n",1-sum); } } if (out==0) return; // // compute sum or histogram // if (out==2) { for (i=0; i<101; i++) hisdif[i]=0; for (i=0; i<10; i++) maxsav[i]=0.0; numone=0; oldsum=0.0; maxdel=0.0; savsum=0; savind=0; width=1; } for (index=start; index<=max; index++) { sum=0; for (i=n; i<=index; i+=n) { // if ((index/i)==limit) // compute d(x) for limit=3, e(x) for limit=4, etc. // printf(" %d %d \n",index,i); if ((index/i)<=limit) break; sum=sum+(int)m[index/i-1]*(int)m[index/i-1]; } newsum=(double)sum; if (sflag!=0) newsum=sqrt((double)sum); printf(" %d %d \n",index,sum); if (out==1) fprintf(Outfp," %e\n",newsum); else { delta=newsum-oldsum; flag=0; if (delta<0.0) { delta=-delta; flag=1; } if (delta>1.0) numone=numone+1; if (delta>maxdel) { for (j=9; j>0; j--) maxsav[j]=maxsav[j-1]; maxsav[0]=delta; maxdel=delta; savsum=sum; savind=index; } oldsum=newsum; if (delta>0.00001) { width=width+1; if (width!=(width/n)*n) { printf("error: width=%d \n",width); goto zskip; } width=0; j=(unsigned int)(delta*(double)bins+0.5); if (j<101) hisdif[j]=hisdif[j]+1; } else width=width+1; } } if (out==2) { printf("maximum difference=%e, sum=%d, index=%d \n",maxdel,savsum,savind); printf("number greater than one=%d \n",numone); for (i=0; i<10; i++) printf(" %e \n",maxsav[i]); j=0; for (i=0; i<=bins; i++) { fprintf(Outfp," %d \n",hisdif[i]); j=j+hisdif[i]; } printf("sum=%d \n",j); } zskip: return; }