Saturday, December 29, 2018

Project Euler Problem 4

Largest palindrome product

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.



public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Problem4.LargestPalindromeProduct(3));
    }
}
class Problem4
{
    internal static double LargestPalindromeProduct(int digits)
    {
        double upperLimit = Math.Pow(10, digits) - 1;
        double lowerLimit = Math.Pow(10, digits - 1) - 1;
        double palindrome = 0;
        for (double number = upperLimit; number > lowerLimit; number--)
        {
            palindrome = double.Parse(MakePalindrome(number.ToString()));
            for (double divisor = upperLimit; divisor > lowerLimit; divisor--)
            {
                if (palindrome > divisor * divisor)
                {
                    break;
                }
                if (palindrome % divisor == 0 && (palindrome / divisor) < upperLimit)
                {
                    return palindrome;
                }
            }
        }
        return palindrome;
    }
    private static string MakePalindrome(string number)
    {
        return number + new string(number.Reverse().ToArray());
    }
}
 

No comments:

Post a Comment

Labels

How to take screenshot of a Webpage using C# Selenium

           ChromeOptions options = new ChromeOptions();             options.AddArgument("headless");//Comment if we want to see ...