/*CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                             C
C  COMPUTE UPPER BOUND MINUS LOWER BOUND (AND ESTIMATED VALUES) 	      C
C  06/05/14 (DKC)							      C
C                                                                             C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC*/
#include <stdio.h>
#include <math.h>
unsigned int euler(unsigned int a);
double mertens2(unsigned int, unsigned int *count, double *sum);
//
unsigned int out=1;  // sum, estimated sum, exceptions, left sum, right sum
double inc=0.0;      // esum+increment
unsigned int base=1;  // Mertens, number of fractions before and after 1/4
//
void main() {
unsigned int N,MAXN;
unsigned int i,j,temp,clsave,crsave,count[2],histop[100],histopn[100];
unsigned int histon[100],histonn[100],histox,histoxn;
double sum,sum1,msum,mlsave,mrsave,lratio,rratio,tsum[2],esum,tmpsum;
double lsum,rsum;
FILE *Outfp;
Outfp = fopen("out1b8.dat","w");
for (i=0; i<100; i++) {
   histop[i]=0;
   histopn[i]=0;
   histon[i]=0;
   histonn[i]=0;
   }
histox=0;
histoxn=0;
MAXN=1280;
for (N=2; N<=MAXN; N++) {
   sum=0.0;
   sum1=0.0;
   for (i=1; i<=N; i++) {
      msum=mertens2(N/i, count, tsum);
      if (i==1) {
	 mlsave=tsum[0];
	 mrsave=tsum[1];
	 clsave=count[0];
	 crsave=count[1];
	 if (base==0)
	    esum=msum;
	 else {
	    esum=0.0;
	    lsum=mlsave;
	    rsum=mrsave;
	    }
//	 printf(" %d %d %e %e \n",clsave,crsave,mlsave,mrsave);
	 }
//
// compute sums
//
      temp=euler(i);
      if ((temp/4)*4!=temp) {
	 sum=sum+msum*(double)i/(double)temp;
	 sum1=sum1+esum*(double)i/(double)temp;
	 }
//    printf(" %d %d %d %e %e \n",i,count[0],N/i,sum,msum);
      if (i!=1) {
	 if (clsave!=0)
	    lratio=(double)count[0]/(double)clsave;
	 else
	    lratio=1.0;
	 if (crsave!=0)
	    rratio=(double)count[1]/(double)crsave;
	 else
	    rratio=1.0;
	 esum=mlsave*lratio+mrsave*rratio;
	 esum=esum*2.0;
	 lsum=lsum+mlsave*lratio;
	 rsum=rsum+mrsave*rratio;
//	 printf(" %e %e %e \n",lsum,rsum,lsum+rsum);
//	 fprintf(Outfp," i=%d %d %d %e %e %e %e %e %e \n",i,count[0],count[1],tsum[0],tsum[1],
//		  lratio,rratio,msum,esum);
//
// histogram (for msum>0)
//
	 if ((msum>0.0)&&(i<=(N/2))) {
	    j=(unsigned int)(msum+0.0001);
	    if ((j<100)&&(j!=0)) {
	       histop[j]=histop[j]+1;
	       if (esum+inc>0.0)
		  histopn[j]=histopn[j]+1;
	       }
	    printf("N=%d, i=%d, N/i=%d, msum=%e, esum=%e \n",N,i,N/i,msum,esum);
	    if (out==2)
	       fprintf(Outfp,"N=%d, i=%d, N/i=%d, msum=%e, esum=%e \n",N,i,N/i,msum,esum);
	    }
//
// histogram (for msum<0)
//
	 if (msum<0.0) {
	    tmpsum=-msum;
	    j=(unsigned int)(tmpsum+0.0001);
	    if ((j<100)&&(j!=0)) {
	       histon[j]=histon[j]+1;
	       if (esum-inc<0.0)
		  histonn[j]=histonn[j]+1;
	       }
	    }
//
// histogram (for msum==0)
//
	 if (msum<0.0)
	    tmpsum=-msum;
	 else
	    tmpsum=msum;
	 j=(unsigned int)(tmpsum+0.0001);
	 if ((j==0)&&(i<=(N/3))) {
	    histox=histox+1;
	    if (esum<0.0)
	       tmpsum=-esum;
	    else
	       tmpsum=esum;
	    j=(unsigned int)(tmpsum+0.1);
	    if (j==0)
	       histoxn=histoxn+1;
	    }
	 }
      }
   if (sum<0.0)
      sum=-sum;
   if (sum1<0.0)
      sum1=-sum1;
   if (out==0)
      fprintf(Outfp," %e\n",sum);
   else {
      if (out==1)
	 fprintf(Outfp," %e\n",sum1);
      else {
	 if (out==3)
	    fprintf(Outfp," %e\n",lsum);
	 if (out==4)
	    fprintf(Outfp," %e\n",rsum);
	 }
      }
   }
if (out==2)
   fprintf(Outfp,"\n");
printf("  %d, %d, %d \n",0,histox,histoxn);
if (out==2)
   fprintf(Outfp,"  %d, %d, %d \n",0,histox,histoxn);
for (i=1; i<20; i++) {
   printf("  %d, %d %d %d %d \n",i,histop[i],histopn[i],histon[i],histonn[i]);
   if (out==2)
      fprintf(Outfp,"  %d, %d %d %d %d \n",i,histop[i],histopn[i],histon[i],histonn[i]);
   }
fclose(Outfp);
return;
}