/*  benchsc_c  by H.L. Schaaf
**  Small-C  version of Duane Parker's benchmark program
**  Ref:  CATS Newsletter Jan 1990  pages 6-8. 
**  Ref:  CATS Newsletter July 1990 pages 10-11
*/

#include <stdio_h>
#define MAX 32767          /* limit of signed integers */

main()
{
char c;
char string[20];               /* characters and/or strings */
int start, stop, index, i, j;  /* handy integer values */
int testfactor, tfsq;          /* testfactor and its square */
int pos, secs, d1[2], d2[2];   /* used in timing and pretty print           */
int minval;                    /* more integers */
minval = 0;
pos = (MAX/100);
testfactor = 181;
tfsq = 32761;

   while( ((minval>0) + (minval<MAX) + (index == 1)) != 3)
   {
   stop = 0;               /* initializing for clean start              */
   index = 1;
   start = 1;
   cls(2);
   printf("This is a Small-C version\n");
   printf("of a benchmark test by DH Parker\n");
   printf("see CATS Newsletters of January and July 1990\n");
   printf("\nStop calculation at what value less than %d ",MAX);
   gets(string);           /* when to stop ?                            */
   puts(string);
   j = strlen(string);
   strncpy(c,string,1);
   start = atoi(c);        /* makes string into integer                 */
   for(i=0 ; i<j; i++)     /* checks for correct input                  */
      {
      c = string[i];
      stop = isdigit(string[i]); /* is it a digit ?             */
      index = stop * index;
      }
   minval = atoi(string);  /* check for proper entry values             */
   if ( j > 5)
      {
      index = 0;
      }
   if(( j == 5) && ( index == 1) && ( start > 3 ))
      {
      index = 0;
      }
   if((minval>=MAX) || (minval<1) || (index == 0))
      {
      printf("\n\nI N V A L I D  ! !"); /* error trapping               */
      printf("\n\nany key to try again\n q to quit");
      c =  getchar();
      switch (c)
         {
         case 'q' :  xcit();     /* 'fancy' form of exit                */
         }
      }                          /* this brace ends getting input data  */
    }
    cls(stdout);           /* clear screen                              */
    printf("Ready to find primes from 32767 to %d\n",minval);
    printf("any key to begin\n");   /* inform and wait for trigger      */
    getchar();
    {                         /* this brace begins output of data       */
    *d1 = date();         /* starting time                          */

    for(index = MAX; index >= minval ; index -= 2)
    {
    while ((pos*100)> index)  /* make newline to group by 100's         */
      {
       --pos ;
      }
    while( index < tfsq )        /* reduce tfsq and testfactor          */
      {                          /*    when  possible                   */
      for( j = 0; j < 4 ; j++)
         {
         tfsq -= testfactor;
         tfsq++;
         }
      testfactor -= 2;
      }
   for( j=3; ( j <= testfactor ); j += 2 )
      {
      if((index % j)==0)         /* not prime                           */
         break;
      }
    }
   }                             /* this brace ends output of data      */
   *d2 = date();             /* checks time of finish               */
   secs = d2[1] - d1[1];
   printf("\nRuntime = %d seconds in Small-C\n",secs);
   printf("\nany key to exit ? ");
   getchar();
   xcit();
}

/* -  -  -  -  -  X C I T -   -  just a 'fancy' show-off exit              */

xcit()
 {
   mode(8);
   cls(2);
   at(stdout,1,8);
   ink(stdout,1);
   printf("It's been good to");
   csize(stdout,3,1);
   at(stdout,3,11);
   ink(stdout,2);
   flash(stdout,1);
   printf("'C'");
   flash(stdout,0);
   csize(stdout,0,0);
   at(stdout,12,15);
   ink(stdout,4);
   printf("you");
   at(stdout,17,5);
   flash(stdout,1);
   ink(stdout,6);
   printf("any key to exit . . ._");
   getchar();
   mode(4);
   flash(stdout,0);
   exit(2);
}
/* The end of the Small-C source code */

