Skip to content

Commit

Permalink
Problems 64-69, lots of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Jun 8, 2010
1 parent 5ad40e8 commit 2925c42
Show file tree
Hide file tree
Showing 58 changed files with 4,166 additions and 159 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
TestResults/
32 changes: 32 additions & 0 deletions Ckknight.ProjectEuler.sln
Expand Up @@ -3,16 +3,48 @@ Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ckknight.ProjectEuler", "Ckknight.ProjectEuler\Ckknight.ProjectEuler.csproj", "{A212B257-6D17-4912-8084-A2B4E9974F62}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ckknight.ProjectEulerTest", "Ckknight.ProjectEulerTest\Ckknight.ProjectEulerTest.csproj", "{45091873-82CF-4F03-A431-4547655C67DD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{611436E3-AF14-4B14-B94F-266F6FA45CA9}"
ProjectSection(SolutionItems) = preProject
Ckknight.ProjectEuler.vsmdi = Ckknight.ProjectEuler.vsmdi
Local.testsettings = Local.testsettings
TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings
EndProjectSection
EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = Ckknight.ProjectEuler.vsmdi
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A212B257-6D17-4912-8084-A2B4E9974F62}.Debug|Any CPU.ActiveCfg = Debug|x86
{A212B257-6D17-4912-8084-A2B4E9974F62}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{A212B257-6D17-4912-8084-A2B4E9974F62}.Debug|Mixed Platforms.Build.0 = Debug|x86
{A212B257-6D17-4912-8084-A2B4E9974F62}.Debug|x86.ActiveCfg = Debug|x86
{A212B257-6D17-4912-8084-A2B4E9974F62}.Debug|x86.Build.0 = Debug|x86
{A212B257-6D17-4912-8084-A2B4E9974F62}.Release|Any CPU.ActiveCfg = Release|x86
{A212B257-6D17-4912-8084-A2B4E9974F62}.Release|Mixed Platforms.ActiveCfg = Release|x86
{A212B257-6D17-4912-8084-A2B4E9974F62}.Release|Mixed Platforms.Build.0 = Release|x86
{A212B257-6D17-4912-8084-A2B4E9974F62}.Release|x86.ActiveCfg = Release|x86
{A212B257-6D17-4912-8084-A2B4E9974F62}.Release|x86.Build.0 = Release|x86
{45091873-82CF-4F03-A431-4547655C67DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45091873-82CF-4F03-A431-4547655C67DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45091873-82CF-4F03-A431-4547655C67DD}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{45091873-82CF-4F03-A431-4547655C67DD}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{45091873-82CF-4F03-A431-4547655C67DD}.Debug|x86.ActiveCfg = Debug|Any CPU
{45091873-82CF-4F03-A431-4547655C67DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45091873-82CF-4F03-A431-4547655C67DD}.Release|Any CPU.Build.0 = Release|Any CPU
{45091873-82CF-4F03-A431-4547655C67DD}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{45091873-82CF-4F03-A431-4547655C67DD}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{45091873-82CF-4F03-A431-4547655C67DD}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Binary file modified Ckknight.ProjectEuler.suo
Binary file not shown.
6 changes: 6 additions & 0 deletions Ckknight.ProjectEuler.vsmdi
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<TestLists xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<TestList name="Lists of Tests" id="8c43106b-9dc1-4907-a29f-aa66a61bf5b6">
<RunConfiguration id="ee51332a-2095-4e7d-83c4-431590abb05b" name="Local" storage="local.testsettings" type="Microsoft.VisualStudio.TestTools.Common.TestRunConfiguration, Microsoft.VisualStudio.QualityTools.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</TestList>
</TestLists>
237 changes: 237 additions & 0 deletions Ckknight.ProjectEuler/BigContinuedFraction.cs
@@ -0,0 +1,237 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace Ckknight.ProjectEuler.Collections
{
public class BigContinuedFraction
{
private BigContinuedFraction(BigInteger floor, IEnumerable<BigInteger> nonPeriodicQuotients, IEnumerable<BigInteger> periodicQuotients)
{
_floor = floor;
_nonPeriodicQuotients = nonPeriodicQuotients ?? Enumerable.Empty<BigInteger>();
_periodicQuotients = periodicQuotients ?? Enumerable.Empty<BigInteger>();
}

public BigContinuedFraction(BigInteger floor)
: this(floor, default(IEnumerable<BigInteger>), default(IEnumerable<BigInteger>)) { }

public BigContinuedFraction(BigInteger floor, IEnumerable<BigInteger> nonPeriodicQuotients)
: this(floor, nonPeriodicQuotients, default(IEnumerable<BigInteger>))
{
if (nonPeriodicQuotients == null)
{
throw new ArgumentNullException("nonPeriodicQuotients");
}
}

public BigContinuedFraction(BigInteger floor, IEnumerable<BigInteger> nonPeriodicQuotients, BigInteger[] periodicQuotients)
: this(floor, nonPeriodicQuotients, (IEnumerable<BigInteger>)periodicQuotients)
{
if (nonPeriodicQuotients == null)
{
throw new ArgumentNullException("nonPeriodicQuotients");
}
else if (periodicQuotients == null)
{
throw new ArgumentNullException("periodicQuotients");
}
}

public static readonly BigContinuedFraction Zero = new BigContinuedFraction(0);
public static readonly BigContinuedFraction One = new BigContinuedFraction(1);

private readonly BigInteger _floor;
public BigInteger Floor
{
get
{
return _floor;
}
}

private readonly IEnumerable<BigInteger> _nonPeriodicQuotients;
public IEnumerable<BigInteger> NonPeriodicQuotients
{
get
{
return _nonPeriodicQuotients;
}
}

private readonly IEnumerable<BigInteger> _periodicQuotients;
public IEnumerable<BigInteger> PeriodicQuotients
{
get
{
return _periodicQuotients;
}
}

public IEnumerable<BigInteger> Quotients
{
get
{
foreach (BigInteger quotient in _nonPeriodicQuotients)
{
yield return quotient;
}

while (true)
{
bool found = false;
foreach (BigInteger quotient in _periodicQuotients)
{
if (!found)
{
found = true;
}
yield return quotient;
}
if (!found)
{
break;
}
}
}
}

private const int MaxNonPeriodicLengthShown = 10;
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append('[');
sb.Append(_floor);

BigInteger[] partialNonPeriodicQuotients = _nonPeriodicQuotients as BigInteger[];
if (partialNonPeriodicQuotients == null)
{
partialNonPeriodicQuotients = _nonPeriodicQuotients.Take(MaxNonPeriodicLengthShown + 1).ToArray();
}
int length = partialNonPeriodicQuotients.Length;
if (length > MaxNonPeriodicLengthShown)
{
length = MaxNonPeriodicLengthShown;
}
for (int i = 0; i < length; i++)
{
if (i == 0)
{
sb.Append("; ");
}
else
{
sb.Append(", ");
}
sb.Append(partialNonPeriodicQuotients[i]);
}
if (partialNonPeriodicQuotients.Length > MaxNonPeriodicLengthShown)
{
sb.Append(", ...");
}

BigInteger[] periodicQuotients = _periodicQuotients as BigInteger[] ?? _periodicQuotients.ToArray();
if (periodicQuotients.Length > 0)
{
if (length == 0)
{
sb.Append("; ");
}
else
{
sb.Append(", ");
}
sb.Append('(');
for (int i = 0; i < periodicQuotients.Length; i++)
{
if (i > 0)
{
sb.Append(", ");
}
sb.Append(periodicQuotients[i]);
}
sb.Append(')');
}

sb.Append(']');
return sb.ToString();
}

public static BigContinuedFraction Sqrt(BigInteger value)
{
BigInteger sqrt = MathUtilities.BigSqrt(value);

if (sqrt * sqrt == value)
{
return new BigContinuedFraction(sqrt);
}
else
{
var period = CollectionUtilities.Repeat(default(object))
.SelectWithAggregate(new { m = BigInteger.Zero, d = BigInteger.One, a = sqrt }, (x, i) =>
{
BigInteger m = x.d * x.a - x.m;
BigInteger d = (value - m * m) / x.d;
BigInteger a = (sqrt + m) / d;
return new { m, d, a };
})
.TakeWhileDistinct()
.Select(x => x.a)
.ToArray();

return new BigContinuedFraction(sqrt, Enumerable.Empty<BigInteger>(), period);
}
}

public static BigContinuedFraction FromBigFraction(BigFraction fraction)
{
if (fraction.Denominator.IsZero)
{
throw new ArithmeticException("Cannot generate continued fraction from NaN, Infinity, or -Infinity");
}

bool first = true;
BigInteger initial = BigInteger.Zero;
List<BigInteger> quotients = new List<BigInteger>();
while (true)
{
BigFraction remainder;
BigInteger floor = BigFraction.DivRem(fraction, BigFraction.One, out remainder).Numerator;

if (first)
{
first = false;
initial = floor;
}
else
{
quotients.Add(floor);
}

if (remainder.IsZero)
{
return new BigContinuedFraction(initial, quotients.ToArray());
}

fraction = BigFraction.Reciprocal(remainder);
}
}

public IEnumerable<BigFraction> GetBigFractions()
{
return this.Quotients
.PrependItem(this.Floor)
.SelectWithAggregate(new { Alpha = BigFraction.Zero, Bravo = BigFraction.PositiveInfinity }, (x, v) =>
new
{
Alpha = x.Bravo,
Bravo = new BigFraction(
v * x.Bravo.Numerator + x.Alpha.Numerator,
v * x.Bravo.Denominator + x.Alpha.Denominator)
})
.Select(x => x.Bravo);
}
}
}

0 comments on commit 2925c42

Please sign in to comment.