Sunday 12 April 2009

PROJECT EULER #39

Link to Project Euler problem 39

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p <= 1000, is the number of solutions maximised?


using System;
using System.Collections.Generic;

namespace project_euler
{
class Program
{
static void Main()
{
//Problem 39
DateTime start = DateTime.Now;
int max = 0,number=0;
Dictionary<int,int> score=new Dictionary<int, int>();
for (int i = 1000; i > 0; i--)
for (int j = i - 1; j > 0; j--)
for (int k = j; k > 0; k--)
if ((k*k + j*j) == (i*i))
if (i + j + k <= 1000)
if (score.ContainsKey(i + j + k))
score[i + j + k] += 1;
else
score.Add(i + j + k, 1);
foreach (KeyValuePair<int, int> pair in score)
if (pair.Value > max)
{
max = pair.Value;
number = pair.Key;
}
Console.WriteLine(number);
TimeSpan time = DateTime.Now - start;
Console.WriteLine("This took {0}", time);
Console.ReadKey();
}
}
}

No comments: