コード例 #1
0
__int64 solve_125()
{
	unsigned* buffer = new unsigned[SQRT_LIMIT + 1];
	for(unsigned i = 1; i < SQRT_LIMIT; ++i)
		buffer[i] = i * i;

	std::set<unsigned> sqrtsum;

	// 依次累加2个数,3个数...为一组的和.并判断是否是回文数,是的话加入set中.
	for(unsigned len = 2; len <= SQRT_LIMIT; ++len)
	{	
		unsigned tmp = std::accumulate(buffer + 1, buffer + len + 1, 0u);
		
		if(tmp > LIMIT)
			break;
		
		if(ispandigital(tmp))
			sqrtsum.insert(tmp);

		for(unsigned j = 2; j <= SQRT_LIMIT; ++j)
		{	// 例如序列abcd, Sum(a, b, c) = x, 那么Sum(b, c, d) = Sum(a, b, c) - a + d = Sum(a, b, c) + (d - a).
			if((tmp += (buffer[j + len - 1] - buffer[j - 1])) > LIMIT)
				break;
			if(ispandigital(tmp))
				sqrtsum.insert(tmp);
		}
	}

	delete[] buffer;
	
	return std::accumulate(sqrtsum.begin(), sqrtsum.end(), 0u);
}
コード例 #2
0
ファイル: p32.c プロジェクト: elly/euler
int main() {
	unsigned int v = 0;
	size_t sum = 0;

	for (size_t i = 0; i < MAX_RHS; ++i)
		if (ispandigital(i))
			sum += i;

	printf("%lu\n", sum);

	return 0;
}
コード例 #3
0
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);
   }