/******************************************************************************/
/*									      */
/*  COMPUTE THE MINIMUM ELEMENT IN A LOOP				      */
/*  03/26/10 (dkc)							      */
/*									      */
/*  This C program finds the minimum element in a loop.  "l" is the number of */
/*  elements in the 3n+c sequence where the next element of the sequence is   */
/*  (3n+c)/2 if n is odd or n/2 if n is even.  "n" is the number of odd       */
/*  elements in the sequence.  The minimum element is found (usually) for     */
/*  some interrelated loop when 2**l-3**n=c.  Some c, l, and n values are;    */
/*									      */
/*     5, 5, 3 (finds minimum element)					      */
/*    13, 8, 5 (finds minimum element in one interrelated loop) 	      */
/*    47, 7, 4 (finds minimum element)					      */
/*    -1, 3, 2 (finds minimum element)					      */
/*   -11, 4, 3 (finds minimum element)					      */
/*   -49, 5, 4 (finds minimum element)					      */
/*  -115, 7, 5 (finds minimum element in one interrelated loop) 	      */
/*									      */
/*   -17, 6, 4 (doesn't find minimum element)                                 */
/*									      */
/******************************************************************************/
#include <stdio.h>
#include <math.h>
int main () {
int c=13;   // c
int l=8;    // length of loop
int n=5;    // number of odd elements in loop
int g,h,j,k,a,b,sum;
double x;
FILE *Outfp;
Outfp = fopen("out14a.dat","w");
printf("l=%d, n=%d, c=%d \n",l,n,c);
sum=0;
for (j=1; j<=l; j++) {
   a=j*n;
   if (a==((a/l)*l))
      a=a/l;
   else
      a=(a/l)+1;
   b=(j-1)*n;
   if (b==((b/l)*l))
      b=b/l;
   else
      b=(b/l)+1;
   k=1;
   k=k<<(j-1);
   if (n<a)
      printf("error \n");
   g=1;
   for (h=0; h<(n-a); h++)
      g=g*3;
   sum=sum+(a-b)*k*g;
   }
printf("sum=%d \n",sum);
k=1;
k=k<<l;
g=1;
for (h=0; h<n; h++)
   g=g*3;
k=k-g;
printf("k=%d \n",k);
x=(double)sum*(double)c/(double)k;
printf("x=%e \n",x);
fclose(Outfp);
return(0);
}