Thursday, December 15, 2011

Project Euler Problem 52 Solution


Problem Statement
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
Solution(bruteforce)
Here is the C++ implementation of this problem.
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>

using namespace std;

int main(){
 long i,j,k,l,n = 1;
 char temp[15],tempp[15];

 while( true ){
  sprintf( temp,"%ld", n * 2);
  sort( temp,temp + strlen(temp) );
  for( i = 3; i <= 6; i++ ){
   sprintf( tempp,"%ld", n * i);   
   sort( tempp,tempp + strlen(tempp) );
   if( strcmp( temp,tempp) )
    break;
  }

  //printf("N>%ld I>%ld\n",n,i);

  if( i == 7)
   break;
  n++;
 }

 printf("%ld\n",n);
 return 0;
}


No comments:

Post a Comment