Skip to content

Commit

Permalink
Problem 75
Browse files Browse the repository at this point in the history
  • Loading branch information
ckknight committed Jun 9, 2010
1 parent 97cb959 commit 1ff2517
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions Ckknight.ProjectEuler/Ckknight.ProjectEuler.csproj
Expand Up @@ -142,6 +142,7 @@
<Compile Include="Problems\Problem072.cs" />
<Compile Include="Problems\Problem073.cs" />
<Compile Include="Problems\Problem074.cs" />
<Compile Include="Problems\Problem075.cs" />
<Compile Include="Problems\ProblemAttribute.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
1 change: 1 addition & 0 deletions Ckknight.ProjectEuler/Collections/IMultiSet.cs
Expand Up @@ -9,5 +9,6 @@ public interface IMultiSet<T> : ICollection<T>
{
int GetCount(T item);
IEnumerable<T> Distinct();
IEnumerable<KeyValuePair<T, int>> GetCounts();
}
}
9 changes: 9 additions & 0 deletions Ckknight.ProjectEuler/Collections/MultiHashSet.cs
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;

namespace Ckknight.ProjectEuler.Collections
{
Expand Down Expand Up @@ -73,6 +74,14 @@ public IEnumerable<T> Distinct()
return _data.Keys;
}

public IEnumerable<KeyValuePair<T, int>> GetCounts()
{
foreach (var pair in _data)
{
yield return pair;
}
}

public void Add(T item)
{
int count;
Expand Down
53 changes: 53 additions & 0 deletions Ckknight.ProjectEuler/Problems/Problem075.cs
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ckknight.ProjectEuler.Collections;

namespace Ckknight.ProjectEuler.Problems
{
[Problem(75,
@"It turns out that 12 cm is the smallest length of wire that can be
bent to form an integer sided right angle triangle in exactly one way,
but there are many more examples.
12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)
In contrast, some lengths of wire, like 20 cm, cannot be bent to form
an integer sided right angle triangle, and other lengths allow more
than one solution to be found; for example, using 120 cm it is possible
to form exactly three different integer sided right angle triangles.
120 cm: (30,40,50), (20,48,52), (24,45,51)
Given that L is the length of the wire, for how many values of
L <= 1,500,000 can exactly one integer sided right angle triangle be
formed?
Note: This problem has been changed recently, please check that you are
using the right parameters.")]
public class Problem075 : BaseProblem
{
public override object CalculateResult()
{
int maximum = 1500000;
return new Range(1, int.MaxValue)
.TakeWhile(m => 2 * m * m + 2 * m <= maximum)
.SelectMany(m => new Range((m % 2 == 0) ? 1 : 2, m, 2)
.Where(n => MathUtilities.AreRelativelyPrime(m, n))
.Select(n => 2 * m * m + 2 * m * n)
.TakeWhile(l => l <= maximum))
.SelectMany(l => new Range(1, int.MaxValue)
.Select(v => l * v)
.TakeWhile(p => p <= maximum))
.ToMultiHashSet()
.GetCounts()
.Count(p => p.Value == 1);
}
}
}
2 changes: 1 addition & 1 deletion Ckknight.ProjectEuler/Program.cs
Expand Up @@ -12,7 +12,7 @@ class Program
{
static void Main(string[] args)
{
RunProblem(74);
RunProblem(75);
}

public static void RunProblem(int number)
Expand Down

0 comments on commit 1ff2517

Please sign in to comment.