Permalink
Browse files

Problems 64-69, lots of refactoring

  • Loading branch information...
unknown
unknown committed Jun 8, 2010
1 parent 5ad40e8 commit 2925c426ade3aaddac052ea117e7e69a6cd95c5e
Showing with 4,166 additions and 159 deletions.
  1. +1 −0 .gitignore
  2. +32 −0 Ckknight.ProjectEuler.sln
  3. BIN Ckknight.ProjectEuler.suo
  4. +6 −0 Ckknight.ProjectEuler.vsmdi
  5. +237 −0 Ckknight.ProjectEuler/BigContinuedFraction.cs
  6. +624 −0 Ckknight.ProjectEuler/BigFraction.cs
  7. +13 −1 Ckknight.ProjectEuler/Ckknight.ProjectEuler.csproj
  8. +13 −0 Ckknight.ProjectEuler/CollectionUtilities.cs
  9. +80 −0 Ckknight.ProjectEuler/Collections/BetterCombinationGenerator.cs
  10. +20 −3 Ckknight.ProjectEuler/Collections/BooleanArray.cs
  11. +292 −0 Ckknight.ProjectEuler/Collections/DefaultDictionary.cs
  12. +121 −0 Ckknight.ProjectEuler/Collections/EnumerableExtensions.cs
  13. +7 −10 Ckknight.ProjectEuler/Collections/FactorGenerator.cs
  14. +12 −0 Ckknight.ProjectEuler/Collections/ImmutableSequence.cs
  15. +209 −14 Ckknight.ProjectEuler/Collections/Int32Set.cs
  16. +1 −1 Ckknight.ProjectEuler/Collections/PrimeFactorGenerator.cs
  17. +159 −99 Ckknight.ProjectEuler/Collections/PrimeGenerator.cs
  18. +251 −0 Ckknight.ProjectEuler/ContinuedFraction.cs
  19. +639 −0 Ckknight.ProjectEuler/Fraction.cs
  20. +99 −7 Ckknight.ProjectEuler/MathUtilities.cs
  21. +1 −1 Ckknight.ProjectEuler/Problems/Problem005.cs
  22. +1 −1 Ckknight.ProjectEuler/Problems/Problem007.cs
  23. +1 −1 Ckknight.ProjectEuler/Problems/Problem010.cs
  24. +2 −2 Ckknight.ProjectEuler/Problems/Problem027.cs
  25. +2 −2 Ckknight.ProjectEuler/Problems/Problem035.cs
  26. +2 −2 Ckknight.ProjectEuler/Problems/Problem037.cs
  27. +1 −1 Ckknight.ProjectEuler/Problems/Problem041.cs
  28. +2 −2 Ckknight.ProjectEuler/Problems/Problem046.cs
  29. +3 −3 Ckknight.ProjectEuler/Problems/Problem049.cs
  30. +2 −2 Ckknight.ProjectEuler/Problems/Problem050.cs
  31. +1 −1 Ckknight.ProjectEuler/Problems/Problem051.cs
  32. +1 −1 Ckknight.ProjectEuler/Problems/Problem058.cs
  33. +3 −3 Ckknight.ProjectEuler/Problems/Problem060.cs
  34. +1 −1 Ckknight.ProjectEuler/Problems/Problem062.cs
  35. +108 −0 Ckknight.ProjectEuler/Problems/Problem064.cs
  36. +79 −0 Ckknight.ProjectEuler/Problems/Problem065.cs
  37. +46 −0 Ckknight.ProjectEuler/Problems/Problem066.cs
  38. +81 −0 Ckknight.ProjectEuler/Problems/Problem067.cs
  39. +79 −0 Ckknight.ProjectEuler/Problems/Problem068.cs
  40. +45 −0 Ckknight.ProjectEuler/Problems/Problem069.cs
  41. +1 −1 Ckknight.ProjectEuler/Program.cs
  42. +62 −0 Ckknight.ProjectEulerTest/BigSqrtTest.cs
  43. +69 −0 Ckknight.ProjectEulerTest/Ckknight.ProjectEulerTest.csproj
  44. +20 −0 Ckknight.ProjectEulerTest/CombinationGeneratorTest.cs
  45. +94 −0 Ckknight.ProjectEulerTest/ContinuedFractionTest.cs
  46. +563 −0 Ckknight.ProjectEulerTest/FractionTest.cs
  47. +35 −0 Ckknight.ProjectEulerTest/Properties/AssemblyInfo.cs
  48. BIN Ckknight.ProjectEulerTest/bin/Debug/Ckknight.ProjectEuler.exe
  49. BIN Ckknight.ProjectEulerTest/bin/Debug/Ckknight.ProjectEuler.pdb
  50. BIN Ckknight.ProjectEulerTest/bin/Debug/Ckknight.ProjectEulerTest.dll
  51. BIN Ckknight.ProjectEulerTest/bin/Debug/Ckknight.ProjectEulerTest.pdb
  52. +14 −0 Ckknight.ProjectEulerTest/obj/Debug/Ckknight.ProjectEulerTest.csproj.FileListAbsolute.txt
  53. BIN Ckknight.ProjectEulerTest/obj/Debug/Ckknight.ProjectEulerTest.dll
  54. BIN Ckknight.ProjectEulerTest/obj/Debug/Ckknight.ProjectEulerTest.pdb
  55. BIN Ckknight.ProjectEulerTest/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
  56. BIN Ckknight.ProjectEulerTest/obj/Debug/ResolveAssemblyReference.cache
  57. +10 −0 Local.testsettings
  58. +21 −0 TraceAndTestImpact.testsettings
View
@@ -0,0 +1 @@
+TestResults/
View
@@ -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
View
Binary file not shown.
@@ -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>
@@ -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);
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 2925c42

Please sign in to comment.