본문 바로가기
Programming/Project Euler

4. Project Euler #4 : Largest palindrome product.

by NoxLuna 2015. 1. 22.

Simply think this problem,

Determine multiplied result of two three-digit numbers a and b is palindrome number and take largest palindrome number.

However, In order to get largest palindrome number, we have to multiply all three-digit numbers each, and the complexitcity can be 100 million order.

So I implemented the algorithm,

1) Generate 6 digit palindron numbers ordered from large to small. (up to 900 complexity)

2) Check if the number can be expressed as the product of two three digits numbers (up to 900)

Although the first and the second end is similar order does not, in fact, is a lot of difference.

The first algorithm is required for this process again to obtain the maximum value. However, the second algorithm, the result is the maximum value that the first match.

Gritty display the program, as follows: Three-digit number that can be multiplied by a for loop to determine Add a few unexpected. The smaller the size you would lose a lot of symmetry. Not because it is actually a dog up to 900 square roots. I can see that far fewer.


int main()
{
        for( int n = 997 ; n > 100 ; n-- )
        {
                int v = n * 1000 + (n/100) + ((n/10)%10)*10 + (n%10)*100;
                for( int i = (v+998)/999 ; i*i <= v ; i++ )
                {
                        if( v%i == 0 )
                        {
                                printf("%dx%d = %d\n", i, v/i, v);
                                return 0;
                        }
                }
        }
}


댓글