Sunday 12 April 2009

PROJECT EULER #32

Link to Project Euler problem 32


We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39 x 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.


using System;
using System.Collections.Generic;

namespace project_euler
{
class Program
{
static void Main()
{
//Problem 55
DateTime start = DateTime.Now;
List<int> products = new List<int>();
string test = "123456789";
int product = 0;
for (int i = 1; i < 2000; i++)
{
string c = i.ToString();
for (int j = 1; j < 50; j++)
{
int temp = i * j;
string d = j.ToString();
string e = temp.ToString();
string s = c + d + e;
if (!s.Contains("0") && s.Length == 9)
{
char[] f = s.ToCharArray();
Array.Sort(f);
string compare = new string(f);
if (compare == test)
{
if (!products.Contains(temp))
{
products.Add(temp);
product += temp;
}
}
}
}
}
Console.WriteLine(product);
TimeSpan time = DateTime.Now - start;
Console.WriteLine("This took {0}", time);
Console.ReadKey();
}
}
}

No comments: