int main(void)
   {
   char msg[200];
   int start_tics, i, j, sq, max=0, count=0, primecnt=0, maxprime=1000000000;
   char *sieve;

   start_tics = clock();

   /* Construct a sieve of Eratosthenes; our limit is a billion, since no     */
   /* pandigital number could have more than nine digits                      */

   sieve = calloc(maxprime+1,          /* allocate space for the sieve        */
               sizeof(char));
   if (sieve == NULL)
       err_exit("error: cannot allocate space for sieve[]!", 1);

   sieve[0] = 0;                       /* 0 and 1 are not primes              */
   sieve[1] = 0;

   for (i=2; i<=maxprime; i++)         /* fill the rest of the sieve          */
      sieve[i] = 1;                    /*  with ones                          */

   sq = sqrt(maxprime);
   for (i=0; i<=sq; i++)               /* for each run of the loop, the first */
      {                                /*  value of i with sieve[i] != 0      */
      if (sieve[i] != 0)               /*   is the next prime number          */
         for (j=i*i; j<=maxprime; j+=i)/*     so strike out all multiples     */
            sieve[j] = 0;              /*      of that prime starting with    */
      }                                /*       its square (lower multiples   */
                                       /*        were taken care of earlier)  */
   for (i=0; i<=maxprime; i++)         /* count the primes we found           */
      if (sieve[i] != 0)
         primecnt++;

   sprintf(msg, "to build the sieve of primes up to %d containing %d primes",
         maxprime, primecnt);
   logcputime(start_tics, msg);

   for (i=1; i<maxprime; i++)          /* now search the primes for           */
      {                                /*  pandigital numbers                 */
      if ( (sieve[i] != 0) && (ispandigital(i)) )
         {
         count++;
         max = i;                      /* the latest found will be the max    */
         }
      }
   sprintf(msg, "there are %d pandigital primes, and the largest is: %d", count, max);
   logcputime(start_tics, msg);
   exit(0);
   }
int main(void)
   {
   char msg[200];
   int i,j, start_tics, savei, savej, max=0;

   start_tics = clock();

   for (i=999; i>99; i--)           /* loop backwards on i                 */
      {
      for (j=999; j>99; j--)        /*  and on j                           */
         {
         if (ispalind(i*j, 10) == 1)
            if (i*j > max)          /* so that we can save time            */
               {                    /*  by moving on to the next j         */
               max = i*j;           /*   once we have a new max with a     */
               savei = i;           /*    given i since we'll never do     */
               savej = j;           /*     better with the same i and a    */
               break;               /*      lower j (thanks Kate!)         */
               }
         }
      }

   sprintf(msg, "Maximal result: %d x %d = %d\n", savei, savej, savei*savej);
   logcputime(start_tics, msg);
   exit(0);
   }
int main(void)
{
    unsigned int count, i, j, max, maxval, start_tics, limit=1000000;
    char msg[200];

    start_tics = clock();
    max=0;                     /* init max length found   */
    for (i=2; i<limit; i++)
    {
        count = 0;
        j = i;
        while (j != 1)
        {
            j = next_coll(j);    /* turn the crank          */
            count ++;            /* count an iteration      */
        }
        if (count > max)
        {
            max = count;        /* save the new max length  */
            maxval = i;         /*  and the value that led  */
        }                   /*  to it                   */
    }

    sprintf(msg, "Longest chain was %d in length starting with %d\n", max, maxval);
    logcputime(start_tics, msg);
    exit(0);
}
int main()
   {
   int start_tics, limit=15000;        /* highest triangle index to try    */
   int i, tr, divs, max=0, maxtr, found;
   extern int *divcnts;
   char msg[250];


   found = 0;
   start_tics = clock();               /* get start time                   */

   buildprimesdivs(limit+1);           /* compile primes, divisor counts   */
   for (i=1; i<limit; i++)
      {
      tr = (i*(i+1))/2;                /* calculate ith triangular number  */
      if (i%2 == 0)
         divs = divcnts[i/2]*divcnts[i+1];
      else
         divs = divcnts[i]*divcnts[(i+1)/2];

      if (divs > max)
         {
         max = divs;
         maxtr = tr;
         if (max > 500)
            {
            sprintf(msg, "%d is the first triangular number with over 500 divisors (it has %d)",
                        tr, max);
            logcputime(start_tics, msg);
            found = 1;                 /* show we found the answer         */
            break;
            }
         }
      }

   if (found == 0)
      {
      sprintf(msg, "Max of %d divisors found for triangular number %d of index %d\n",
         max, maxtr, i);
      logcputime(start_tics, msg);
      }
   exit(0);
   }
Ejemplo n.º 5
0
int main()
   {
   int i, start_tics, limit=1000000;
   char msg[200];

   start_tics = clock();                  /* record start time                   */

   for (i=1; i<limit; i++)
      {
      if (arepermuted(6, i, 2*i, 3*i, 4*i, 5*i, 6*i) == 1)
         {
         sprintf(msg, "Found a success: %d  with multiples: %d %d %d %d %d",
                    i, 2*i, 3*i, 4*i, 5*i, 6*i);
         logcputime(start_tics, msg);
         exit(0);
         }
      }

   sprintf(msg, "No success found for values up to %d", limit);
   logcputime(start_tics, msg);
   exit(0);
   }
Ejemplo n.º 6
0
int main(void)
   {
   int limit = 1000000;
   char msg[150];
   int start_tics;
   extern int *primes, primecnt;

   start_tics = clock();                  /* record starting time       */
   buildprimes(limit);                    /* build the table of primes  */

   if (primecnt < 10001)
      {
      printf("Limit of %d is too small; look further!\n", limit);
      exit(1);
      }

   sprintf(msg, "The 10,001st prime is: %d", primes[10000]);
   logcputime(start_tics, msg);
   exit(0);
   }
int main(void)
   {
   extern int *primes;
   extern int primecnt;
   char msg[150];
   int limit = 2000000, sum=0, billions=0, start_tics, k;

   start_tics = clock();                  /* record starting time       */
   buildprimes(limit);                    /* build the table of primes  */

   for (k=0; k<primecnt; k++)
      {
      sum += primes[k];
      while (sum >= 1000000000)           /* we're working with 32-bit  */
         {                                /*  arithmetic, so handle     */
         billions++;                      /*   the carry                */
         sum -= 1000000000;
         }
      }
   sprintf(msg, "The sum was: %d%09d\n", billions, sum);
   logcputime(start_tics, msg);
   exit(0);
   }
Ejemplo n.º 8
0
int main(void)
   {
   int limit=100000000;
   long long count=0;
   int i, j, start_tics;
   extern int primecnt, *primes;
   char msg[150];

   start_tics = clock();

   buildprimes(limit/2);                     /* build primes up to half the limit         */

   for(i=0; i<primecnt; i++)                 /* for all primes up to half the limit       */
      for (j=0; j<=i; j++)                   /*  see if the product is within the limit   */
         {
         if (primes[i]*primes[j] >= limit)   /* if product has reached the limit          */
            break;                           /*  we're done for this i                    */
         count++;                            /* else count another semi-prime             */
      }

   sprintf(msg, "to determine the answer is %I64d", count);
   logcputime(start_tics, msg);
   exit(0);
   }
int main(int argc, char **argv)
   {
   int digits[1000];                               /* digits read from file      */
   int digitcnt=0, linecnt=0, start_tics;
   int i, j, maxidx;
   unsigned int bilmax=0, max=0, product, bilproduct;
   char line[1000], *cp;                           /* read-in area               */
   char msg[200];                                  /* output message space       */
   FILE *fp=NULL;
   char filename[]="C:\\Documents and Settings\\Mel\\My Documents\\cworkspace\\euler\\src\\thousand_digits.txt";

   start_tics = clock();

   fp = fopen(filename, "r");                      /* open input file            */
   if (fp == NULL)
      {
      perror("Error opening file");
      exit(0);
      }

   while (1)
      {
      cp = fgets(line, 200, fp);                   /* read a line                */
      if (cp == NULL)                              /* if EOF just end the loop   */
         break;
      linecnt++;

      for (i=0; i<strlen(line); i++)
         {
         if (line[i] != '\n')                      /* skip out on the linefeed     */
            digits[digitcnt++] = line[i] - '0';    /* move the character into array*/
         }
      }

   sprintf(msg, "We read %d lines and %d digits", linecnt, digitcnt);
   lognote(msg);

   for (i=0; i<987; i++)                           /* for each 13 digit sequence   */
      {
      product = 1;
      bilproduct = 0;
      for (j=0; j<13; j++)
         {
         product *= digits[i+j];
         bilproduct *= digits[i+j];
         if (product >= 100000000)
            {
            bilproduct += (product/100000000);
            product %= 100000000;
            }
         }
      if (   ( bilproduct > bilmax)
          || (  (bilproduct == bilmax) && (product > max) ) )
         {
         bilmax = bilproduct;
         max = product;
         maxidx = i;
         }
      }
   sprintf (msg, "Max was: %d%08d at index %d\n", bilmax, max, maxidx);
   logcputime(start_tics, msg);
   exit(0);
   }
int main(int argc, char **argv)
   {
   int values[20][20] = {8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8,
                        49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0,
                        81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65,
                        52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91,
                        22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80,
                        24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50,
                        32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70,
                        67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21,
                        24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72,
                        21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95,
                        78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92,
                        16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57,
                        86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58,
                        19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40,
                         4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66,
                        88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69,
                         4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36,
                        20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16,
                        20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54,
                         1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48};

   unsigned int hmax=0, vmax=0, d1max=0, d2max=0, product;
   int i,j, max=0, start_tics;
   char msg[250];

   start_tics = clock();            /* record start time          */

   /* first figure max for horizontals                            */

   for (i=0; i<20; i++)             /* for all rows               */
      {
      for (j=0; j<17; j++)          /* for cols from 0 to 17      */
         {
         product =  values[i][j]*values[i][j+1]*values[i][j+2]*values[i][j+3];
         if (product > hmax)
            hmax = product;
         }
      }

   /* second figure max for verticals                             */

   for (i=0; i<17; i++)             /* for rows 0 through 17      */
      {
      for (j=0; j<19; j++)          /* and all columns            */
         {
         product =  values[i][j]*values[i+1][j]*values[i+2][j]*values[i+3][j];
         if (product > vmax)
            vmax = product;
         }
      }

   /* third figure max for diagonals going down to the right      */

   for (i=0; i<17; i++)             /* for rows 0 through 17      */
      {
      for (j=0; j<17; j++)          /* and columns 0 through 17   */
         {
         product =  values[i][j]*values[i+1][j+1]*values[i+2][j+2]*values[i+3][j+3];
         if (product > d1max)
            d1max = product;
         }
      }

   /* fourth figure max for diagonals going down to the left      */

   for (i=0; i<17; i++)
      {
      for (j=3; j<17; j++)
         {
         product =  values[i][j]*values[i+1][j-1]*values[i+2][j-2]*values[i+3][j-3];
         if (product > d2max)
            d2max = product;
         }
      }

   if (hmax > max)               /* figure the overall maximum    */
      max = hmax;
   if (vmax > max)
      max = vmax;
   if (d1max > max)
      max = d1max;
   if (d2max > max)
      max = d2max;

   sprintf(msg, "Max product was: %d\n", max);
   logcputime(start_tics, msg);
   exit(0);
   }
int main()
   {
   int start_tics;
   unsigned int i, k, sum=0, sq, count=0;
   char msg[100];
   int sumdivs[LIMIT];                         /* proper divisor sum for each i   */

   start_tics = clock();                       /* get start cpu time              */

   sumdivs[0] = 0;
   sumdivs[1] = 0;

   /*
    * work out the table of the sum of proper divisors for each integer
    */

   for (i=2; i<LIMIT; i++)
      {
      sq = sqrt(i);                             /* look for divisors up to the       */
      sum = 1;                                  /*  square root; count the divisor 1 */
      for (k=2; k<=sq; k++)                     /*  start with 2 so we avoid         */
         {                                      /*   counting i itself               */
         if (i%k == 0)
            {
            sum += k;
            sum += i/k;
            }
         }

      if (sq*sq == i)                           /* if it is a perfect square         */
         sum -= sq;                             /*  don't count the sqrt twice       */
      sumdivs[i] = sum;                         /*   save the sum                    */
      }

   /*
    * Search the table for amicable pairs
    */

   sum = 0;
   for (i=2; i<LIMIT; i++)
      {
      if (sumdivs[i] >= LIMIT)                /* ignore if number of divisors     */
         continue;                            /*  is too big                      */
      if (sumdivs[sumdivs[i]] == i)           /* if amicable                      */
         {
         if (i != sumdivs[i])                 /* if two distinct numbers          */
            {
            sprintf(msg, "Amicable number found: %d paired with %d", i, sumdivs[i]);
            lognote(msg);
            sum += i;
            count++;
            }
         else
            {
            sprintf(msg, "Perfect number found: %d", i);
            lognote(msg);
            }
         }
      }

   sprintf(msg, "to find %d amicable numbers with sum %d\n", count, sum);
   logcputime(start_tics, msg);               /* report elapsed time              */
   exit(0);
   }