Skip to main content
added 29 characters in body
Source Link
Denis
  • 8.6k
  • 5
  • 33
  • 76

I'd like to suggest an alternative solution as your's looks over-complicated and it's hard to follow your logic.

My solution offers better performance, better readability and more OO lookmore OO look better code structure.

We can start by defining a function that determines whether a number is a palindrome or not:

public static bool IsPalindrome(string input)
{
    for (int i = 0; i <= input.Length / 2; i++)
    {
        if (input[i] != input[input.Length - 1 - i])
        {
            return false;
        }
    }
    return true;
}

With this our main logic becomes trivial:

private static void Main()
{
    int max = 0;
    for (int i = 100; i < 1000; i++)
    {
        for (int j = i + 1; j < 1000; j++)
        {
            if (max < i * j && IsPalindrome((i * j).ToString()))
            {
                max = i * j;
            }
        }
    }
    Console.WriteLine(max);
}

I'd like to suggest an alternative solution as your's looks over-complicated and it's hard to follow your logic.

My solution offers better performance, better readability and more OO look.

We can start by defining a function that determines whether a number is a palindrome or not:

public static bool IsPalindrome(string input)
{
    for (int i = 0; i <= input.Length / 2; i++)
    {
        if (input[i] != input[input.Length - 1 - i])
        {
            return false;
        }
    }
    return true;
}

With this our main logic becomes trivial:

private static void Main()
{
    int max = 0;
    for (int i = 100; i < 1000; i++)
    {
        for (int j = i + 1; j < 1000; j++)
        {
            if (max < i * j && IsPalindrome((i * j).ToString()))
            {
                max = i * j;
            }
        }
    }
    Console.WriteLine(max);
}

I'd like to suggest an alternative solution as your's looks over-complicated and it's hard to follow your logic.

My solution offers better performance, better readability and more OO look better code structure.

We can start by defining a function that determines whether a number is a palindrome or not:

public static bool IsPalindrome(string input)
{
    for (int i = 0; i <= input.Length / 2; i++)
    {
        if (input[i] != input[input.Length - 1 - i])
        {
            return false;
        }
    }
    return true;
}

With this our main logic becomes trivial:

private static void Main()
{
    int max = 0;
    for (int i = 100; i < 1000; i++)
    {
        for (int j = i + 1; j < 1000; j++)
        {
            if (max < i * j && IsPalindrome((i * j).ToString()))
            {
                max = i * j;
            }
        }
    }
    Console.WriteLine(max);
}
Source Link
Denis
  • 8.6k
  • 5
  • 33
  • 76

I'd like to suggest an alternative solution as your's looks over-complicated and it's hard to follow your logic.

My solution offers better performance, better readability and more OO look.

We can start by defining a function that determines whether a number is a palindrome or not:

public static bool IsPalindrome(string input)
{
    for (int i = 0; i <= input.Length / 2; i++)
    {
        if (input[i] != input[input.Length - 1 - i])
        {
            return false;
        }
    }
    return true;
}

With this our main logic becomes trivial:

private static void Main()
{
    int max = 0;
    for (int i = 100; i < 1000; i++)
    {
        for (int j = i + 1; j < 1000; j++)
        {
            if (max < i * j && IsPalindrome((i * j).ToString()))
            {
                max = i * j;
            }
        }
    }
    Console.WriteLine(max);
}