Project Euler : 52
June 15, 2009 Leave a comment
125874 * 2 = 251748
125874 and 251748 are permuted, bcoz these two numbers contain same digits.
How can we check that, two numbers r permutation of each other or not ?
A simple algorithm would be,
split those two numbers in an array
sort them
and compare
splitting a number can be done either continuously MOD the number with 10
then divide it with 10 and assigned the result to itself
while ( number ) {
digit [ k++ ] = number % 10;
number /= 10;
}
now sort the array, and compare
it two arrays r equal then they r permutation of each other
or, u can use string.
Here is a sample code to check permutation.
#include "stdio.h"
#include "algorithm"
#include "string.h"
using namespace std;
int main ()
{
char a [15], b [15];
printf ("Enter First Number: ");
scanf ("%s", a);
printf ("Enter Second Number: ");
scanf ("%s", b);
sort ( a, a + strlen ( a ) );
sort ( b, b + strlen ( b ) );
if ( strcmp ( a, b ) == 0 )
printf ("Permutation\n");
else
printf ("Not a Permutation\n");
return 0;
}
Ranges : (our required ans. Would be … )
Below 100 :: the ans. Must not exist here
Below 1000 :: 100 – 166
Below 10000 :: 1000 – 1666
Below 100000 :: 10000 – 16666
Below 1000000 :: 100000 – 166666
Interestingly the final ans is a permutation of the two numbers which r given in sample examples.


