コード例 #1
0
ファイル: prob36.cpp プロジェクト: cbartol/Project-Euler
int main(){
	int sum, i;
	
	for(sum = 0, i=0; i < LIMIT ; i++){
		if(isPalindromic(int_to_vector(i)) && isPalindromic(to_binary(i)) )
			sum += i;
	}
	std::cout << "Answer: " << sum << std::endl;
	return 0;
}
コード例 #2
0
void problem36(){
  int i, sum=0;
  for(i=1; i<1000000; i++){
    if(i==3) printf("%d\t \n", isPalindromic(3));
    if(isPalindromic(i)){
      if(i==3) printf("adding\n");
      printf("Decimal: %d\tBinary: %s\n", i, intToString(i, 2));
      sum+=i;
      if(i==3) printf("added\n");
    }
  }
  printf("\nSum of all numbers, less than one million, which are palindromic in base 10 and base 2 == %d\n", sum);

  printf("\nisPalindromic: %d\n", isPalindromic(3));
}
コード例 #3
0
int projectEuler_004() {
	int ret = 0;
	int start = 999;
	int jump = 1;

	for (int i = 999; i >= 100; i--) {
		if (i % 11 == 0) {
			//	then we should check every integers for j
			start = 999;
			jump = 1;
		}
		else {
			//	now, j should be multiple of 11
			start = 990;
			jump = 11;
		}
		for (int j = start; j >= i; j = j - jump) {
			if (i * j <= ret) {
				break;
			}
			if (isPalindromic(i * j)) {
				ret = i * j;
			}
		}
	}

	return ret;
}
コード例 #4
0
int main()
{
    int nmax = 0;
    for(int i = 101; i< 1000; ++i){
        if(i%10 ==0) continue;
        for(int j = i; j< 1000; ++j) {
            if((i %2 == 0 && j % 5 == 0) || (i%5==0 && j % 2 ==0)) continue;
            int num = i * j;
            if(nmax < num && isPalindromic(num, 10) )
                nmax = num;
            
        }
    }
    printf("%d\n", nmax);
}
コード例 #5
0
ファイル: problem4.c プロジェクト: lab2/C-ProjectEuler
int main(){
  int i, j, max=0;
  clock_t start = clock();
  for (i=100;i<=999;i++){
    for (j=100;j<=999;j++){
      if (isPalindromic(i*j)==true){
        if (i*j>max)
          max=i*j;
      }
    }
  }
  printf("%d\n", max);
  printf("\nTime: %g sec", ((double)clock() - start) / (double)CLOCKS_PER_SEC);
  return 0;     
}
コード例 #6
0
ファイル: palsquare.c プロジェクト: waitin2010/Algorithm
int main(int argc, char** argv)
{
	FILE *fpin = fopen("palsquare.in","r");
	FILE *fpout = fopen("palsquare.out","w");
	char *str;
	int i;
	fscanf(fpin,"%d",&Base);
	for(i = 1; i <= 300; i++)
	{
	  str = toBaseString(i*i);
	  if(isPalindromic(str))
		{
			fprintf(fpout,"%s ",revert(toBaseString(i)));
			fprintf(fpout,"%s\n",toBaseString(i*i));
	    }
	}
	return 0;
}
コード例 #7
0
 vector<vector<string> > partition(string s) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     vector<vector<bool> > isPalindromic(s.length(), vector<bool>(s.length()));
     for (int i = 0; i < s.length(); i++) {
         isPalindromic[i][i] = true;
     }
     
     for (int k = 1; k < s.length(); k++) {
         for (int i = 0; i < s.length()-k; i++) {
             int j = i+k;
             isPalindromic[i][j] = (s[i] == s[j] && (j - i <= 2 || isPalindromic[i+1][j-1]));
         }
     }
     
     return helper(s, 0, isPalindromic);
     
 
 }
コード例 #8
0
ファイル: problem_125.c プロジェクト: mryingster/ProjectEuler
int main()
{
    printf("Project Euler - Problem 125:\n"
           "Find the sum of all numbers less than 10^8 that are both palindromic and can be written as the sum of consecutive squares.\n\n");

    // Begin time tracking
    struct timeval start;
    gettimeofday(&start, NULL);

    unsigned long long sum = 0;
    unsigned long long limit = pow(10, 8);
    int squarelim = sqrt(limit)+1;
    bool *history = malloc(limit*sizeof(bool));
    bool debug = false;

    // Find sums
    for (int start=1; start<squarelim; start++)
    {
        unsigned long long n = start * start;
        for (int end=start+1; end<squarelim; end++)
        {
            n += end * end;
            if (n >= limit) break;
            if (isPalindromic(n))
            {
                if (history[n] == false)
                {
                    history[n] = true;
                    sum += n;
                    if (debug == true)
                        printf("%llu\n", n);
                }
            }
        }
    }

    printf("Sum: %llu\n", sum);
    printElapsedTime(start);

    free(history);
    return 0;
}
コード例 #9
0
ファイル: main.cpp プロジェクト: zmeadows/project-euler
int main(int argc, char** argv) {

    unsigned long long int sum, track;
    track = 0;
    sum = 0;
    for (unsigned long long int a = 1; a < 10000; a++){
        sum = a*a;
        for (unsigned long long int b = a + 1; b < 10000; b++){
            sum += b*b;
            if (sum > 100000000)
                break;
            if (isPalindromic(sum) && isNotIn(sum))
                psqsums.push_back(sum);
        }
    }

    for (int q = 0; q < psqsums.size(); q++){
        track += psqsums[q];
    }
    cout << track << endl;
    return 0;
}
コード例 #10
0
ファイル: solve.cpp プロジェクト: itroot/projecteuler
int main(int /*argc*/, char* /*argv*/[])
{
    NumberArray numberArray;
    std::copy(Counting(0), Counting(upperLimitSqrt), numberArray.begin());
    NumberArray squareArray;
    std::transform(numberArray.begin(), numberArray.end(), squareArray.begin(), boost::math::pow<2, Number>);
    NumberArray squareSumArray;
    Number sum = 0;
    for_each_indexed(squareArray.begin(), squareArray.end(), [&](Number n, size_t i) {
        sum += n;
        squareSumArray[i] = sum;
    });
    //Number result = 0;
    std::set<Number> resultSet;
    for_distinct_collection(squareSumArray.begin(), squareSumArray.end(), 2,
        [&](std::vector<NumberArray::iterator>::iterator begin, std::vector<NumberArray::iterator>::iterator end)
        {
            Number number = **(begin+1) - **begin;
            if (number >=  upperLimit) {
                return;
            }
            std::string s = boost::lexical_cast<std::string>(number);
            if (1 == (*(begin+1) - *begin)) {
                return;
            }
            if (isPalindromic(s.begin(), s.end())) {
                //std::cout << *begin - squareSumArray.begin() << " " << *(begin+1) - squareSumArray.begin() << std::endl;
                //std::cout << "@ " << number << std::endl;
                //std::cout << std::endl;
                resultSet.insert(number);
                //result += number;
            }
        }
    );
    std::cout << std::accumulate(resultSet.begin(), resultSet.end(), 0L) << std::endl;
}