/*****************************************************************************/
/*									     */
/*  COUNT PAIRS OF CONSECUTIVE ROOTS OF THE CONGRUENCE X**((P-1)/N)=Y(MOD P) */
/*  06/01/07 (dkc)							     */
/*									     */
/*  This C program tests propositions for n=4.				     */
/*									     */
/*****************************************************************************/
#include <stdio.h>
#include <math.h>
#include "input.h"
unsigned int croots(unsigned int *input, unsigned int index, unsigned int n,
		    unsigned int *output);
unsigned int r[100000];
int main () {
unsigned int n=4;		 // n value
unsigned int h,i,sum,p,p1,pn,flag0,flag1,temp,count;
unsigned int s[14];
FILE *Outfp;
Outfp = fopen("out4.dat","w");
count=0;
for (h=0; h<insize; h++) {
   p=input[2*h];		   // load p
   if (p>40000) 		   // save execution time
      break;
   i=croots(input, h, n, s);	   // count consecutive roots of congruences
   if (i==0)			   // continue if n does not divide p-1
      continue;
   if (i==2) {
      printf(" error: bad primitive root \n");
      break;
      }
   printf(" p=%d ",input[2*h]);
/***************************************************/
/* check if the sum of S[i] is equal to (p-1)/n-1  */
/***************************************************/
   sum=0;
   for (i=0; i<n; i++) {
      sum=sum+s[i];
      printf(" %d ",s[i]);
      }
   printf("\n");
   p1=p-1;
   pn=p1/n;
   if (sum!=pn-1) {
      printf(" error: incorrect sum \n");
      break;
      }
/********************************************************/
/*  check if s[1]-s[2]=s[2]-s[3] when (p-1)/n is even	*/
/********************************************************/
   if (pn==(pn/2)*2) {
      if ((s[0]-s[1])!=(s[1]-s[2])) {
	 printf(" error: differences not equal \n");
	 fprintf(Outfp," error: differences not equal \n");
	 }
      }
/**********************************/
/*  check if s[1]=s[2]=s[3]=s[4]  */
/**********************************/
   flag0=1;
   for (i=1; i<n; i++) {
      if (s[i]!=s[0])
	 flag0=0;
      }
/***********************************/
/*  check if (p-1)/n is a square   */
/***********************************/
   flag1=0;
   temp=(unsigned int)(sqrt((double)pn)+0.01);
   if (temp*temp==pn)
      flag1=1;
/****************************************************************/
/*  check if (p-1)/n is an odd square when s[0]=s[1]=s[2]=s[3]	*/
/****************************************************************/
   if (flag0==1) {
      fprintf(Outfp," p=%d ",p);
      for (i=0; i<n; i++)
	 fprintf(Outfp," %d ",s[i]);
      fprintf(Outfp,"\n");
      if ((flag1==0)||((pn/2)*2==pn)) {
	 fprintf(Outfp,"p=%d, error: (p-1)/n is not an odd square \n",p);
	 printf(" error: (p-1)/n is not an odd square \n");
	 }
      }
/*****************************************************************/
/*  check if s[1]=s[2]=s[3]=s[4] when (p-1)/n is an odd square	 */
/*****************************************************************/
   if (flag1==1) {
      if ((pn/2)*2!=pn) {
	 if (flag0==0) {
	    fprintf(Outfp,"p=%d, error: unequal s values \n",p);
	    printf(" error: unequal s values \n");
	    }
	 }
      }
/******************************/
/* count sums divisible by n  */
/******************************/
   if ((pn-1)==((pn-1)/n)*n)
      count=count+1;
   }
fprintf(Outfp," count=%d \n",count);
fclose(Outfp);
return(0);
}