Project Euler – Problem 4

The problem description, from the Project Euler website:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 ×99.

Find the largest palindrome made from the product of two 3-digit numbers.

Again, a little brute force never hurt anybody 😛

General Algorithm

Before you begin, you must identify how exactly your going to check if a number is a palindrome. The question defines a palindrome for you: A palindromic number reads the same both ways. IMO, the easiest way to check if a number is a palindrome is to reverse it and see if it is equal to it’s original value. Given below is a general reverse algorithm.

  1. Initialize reversed number to zero.
  2. While the number to be reversed (n), is not zero:
    1. Get the last digit by n % 10.
    2. Make it a subsequent digit of the reversed number by doing the following:
      1. Reversed number  *= 10.
      2. Reversed Number += last digit.
    3. Remove the last digit (n /= 10).
  3. At the end of the loop, you will have the reversed number.

I guess the iteration for checking the product of every 3 digit number needs no explanation.

Solution in C++

Note: If you’re having trouble with _int64 on your compiler, replace it with long long int

Advertisements
Leave a comment

2 Comments

  1. Project Euler seems suspiciously similar to the CBSE course, don’t you think? 😛

    Reply
    • virajmahesh

       /  January 16, 2012

      Fibonacci numbers are the same, whether it’s cbse or Euler. And the problems become difficult later on.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: