3

I am using this code to solve problem. 4 on Project Euler. The problem reads:

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.

I have tried to solve the problem. My code returns a palindrome number and a fairly big one too. But it is incorrect. I have also found a correct solution at MathBlog.dk. But what is wrong with my code? --

long result = 0;
for (long x = 100; x <= 999; x++)
{
    for (long y = 100; y <= 999; y++)
    {
        long num = x * y;
        if (num.ToString() == StringFunctions.ReverseString(num.ToString()))
        {
                result = num;
        }
    }
}
Console.WriteLine("Result : {0}", result);

The StringFunctions.RevereString function is as follows:

public static class StringFunctions
{
    public static string ReverseString(string s)
    {
        string result = "";
        for (int i = s.Length - 1; i >= 0; i--)
        {
            result += s.Substring(i, 1);
        }
        return result;
    }
}

My code returns 580085 but the correct answer is 906609. I don't want to know about better solutions. I just want to know why my code isn't working.

Any help will be much appreciated. Thanks in advance.

Lews Therin
  • 3,707
  • 2
  • 27
  • 53
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
  • 1
    You never check whether the newly calculated result is greater than than the current result. It may be that you're overwriting your highest value result with a lower value in your for loops. – Robert Lacher Oct 23 '15 at 11:44
  • 2
    You are returning the **last** palindrome found, not the **biggest** one. – Dennis_E Oct 23 '15 at 11:44
  • @rob and Dennis_E How could I even have missed that? Thanks for your help. – Fᴀʀʜᴀɴ Aɴᴀᴍ Oct 23 '15 at 11:46
  • 5
    Too bad you don't want to know about better solutions :-) – Dennis_E Oct 23 '15 at 11:47
  • 1
    @Dennis_E that's because I have already found too many just by googling "Project Euler Solution to Problem 4". They solve the problem in ~0-1 ms. If you have an even better solution, I welcome you to let me know of it. – Fᴀʀʜᴀɴ Aɴᴀᴍ Oct 23 '15 at 11:48
  • 2
    @FarhanAnam That makes sense. Solutions of the older problems can be found anywhere. – Dennis_E Oct 23 '15 at 11:54
  • a faster way would be to start from 999 to 100. and the condintion `if (num > max && num.ToString() == num.Reversed)` will not check to reverse if num is smaller than biggest found number. – M.kazem Akhgary Oct 23 '15 at 11:55

1 Answers1

2

Your result is storing the last palindrome number obtained from the loop but not the largest one.

Both the variables X and Y iterate from 100 to 999

Imagine a case when (assuming all obtained numbers are palindromes) x = 500 and y = 500. It will execute earlier than x = 990 and y = 100. But in the earlier case the palindrome is larger but your code stores the smaller one. Use an if condition to get the largest palindrome:

long result = 0;
for (long x = 100; x <= 999; x++)
{
    for (long y = 100; y <= 999; y++)
    {
        long num = x * y;
        if (num.ToString() == StringFunctions.ReverseString(num.ToString()))
        {
            if(result < num)
            {
            result = num;
            }
        }
    }
}
Console.WriteLine("Result : {0}", result);
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
amit dayama
  • 3,246
  • 2
  • 17
  • 28