Tuesday 2 February 2016

Project Euler #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

Solution

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
         int testCases = Convert.ToInt32(Console.ReadLine());
            List<int> numbers = new List<int>();
            for (int k = 0; k < testCases; k++)
            {
                numbers.Add(Convert.ToInt32(Console.ReadLine()));
            }
            foreach (int number in numbers)
            {
                Console.WriteLine(FindLargestNumber(number));
            }
    }
    public static int FindLargestNumber(int number)
        {
            for (int x = 9; x >= 0; --x)
            {
                int a = x * 100001;
                for (int y = 9; y >= 0; --y)
                {
                    int b = a + y * 10010;
                    for (int z = 9; z >= 0; --z)
                    {
                        int n = b + z * 1100;
                        if (n < number)
                        {
                            for (int i = 990; i > 99; i -= 11)
                            {
                                if (n % i == 0)
                                {
                                    int t = n / i;
                                    if (t < 1000) return n;
                                }
                            }
                        }
                    }
                }
            }
            return 0;
        }
}

No comments:

Post a Comment