Problem Statement
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.
Solution
A simple problem with a simple solution. Multiply the numbers from 100 to 999 amongst themselves. Reverse the product and check if the original product is equal to the reversed product. If they are equal then save the product in the output variable. Before saving in the output variable check whether the original value of the output variable is less than the value of the product. If yes only then save the product to the output variable.
Code
Code Explanation
The code can be explained as follows:
1. Obtain the lower range and the upper range.
2. Initialize a loop starting with lower range entered above upto higher range.
3. Initialize a second loop with the exact same parameters as the above loop.
4. Find the product of the two loops and then reverse the product.
5. If product and reverse of product are equal then check store it in the output variable, provided that the value of the product is larger than the value of the output variable.
Short Code
How To Use
The call should be made like with the parameter ?l=100 and ?u=1000. The ?l is to specify the lower range and ?u to specify the upper range.
A sample call can be made like this http://www.Euler_004.php?l=100&?u=1000
No comments:
Post a Comment