Skip to content

Commit

Permalink
Add Problem009 and refactor Problem007
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberpunk2099 committed Jun 6, 2019
1 parent 56e66b7 commit 8d085fa
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 34 deletions.
33 changes: 33 additions & 0 deletions src/ProjectEuler/ProjectEuler.Problems/Helpers/Funcs.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
using System;
using System.Linq;
using static System.String;
using static System.Environment;
using static System.Math;

namespace ProjectEuler.Problems.Helpers
{
public static class Funcs
{
/// <summary>
/// Sieve of Eratosthenes
/// <see cref="http://www.wikizero.biz/index.php?q=aHR0cHM6Ly9lbi53aWtpcGVkaWEub3JnL3dpa2kvU2lldmVfb2ZfRXJhdG9zdGhlbmVz"/>
/// </summary>
public static int[] Sieve(int upperBound)
{
if (upperBound < 2) return new int[0];

var isComposite = new bool[upperBound + 1];
var primes = new int[upperBound / (int)Log10(upperBound)];

isComposite[0] = isComposite[1] = true;

for (var i = 2; i * i <= upperBound; i++)
{
if (isComposite[i]) continue;
for (var j = i * i; j <= upperBound; j += i)
isComposite[j] = true;
}

var p = 0;
for (var k = 0; k <= upperBound; k++)
{
if (isComposite[k]) continue;
primes[p] = k;
p++;
}

return primes.TakeWhile(i => i > 0).ToArray();
}

public static string ToText(this int[] values)
{
const int colSize = 7;
Expand Down
36 changes: 2 additions & 34 deletions src/ProjectEuler/ProjectEuler.Problems/Solutions/Problem007.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq;
using ProjectEuler.Problems.Helpers;
using static System.Math;
using ProjectEuler.Problems.Helpers;
using static ProjectEuler.Problems.Helpers.Funcs;

namespace ProjectEuler.Problems.Solutions
{
Expand All @@ -21,36 +20,5 @@ public class Problem007 : IProblem
"What is the 10 001st prime number?";

public string GetSolution() => Sieve(UpperBound).ToText();

/// <summary>
/// Sieve of Eratosthenes
/// <see cref="http://www.wikizero.biz/index.php?q=aHR0cHM6Ly9lbi53aWtpcGVkaWEub3JnL3dpa2kvU2lldmVfb2ZfRXJhdG9zdGhlbmVz"/>
/// </summary>
private static int[] Sieve(int upperBound)
{
if (upperBound < 2) return new int[0];

var isComposite = new bool[upperBound + 1];
var primes = new int[upperBound / (int)Log10(upperBound)];

isComposite[0] = isComposite[1] = true;

for (var i = 2; i * i <= upperBound; i++)
{
if (isComposite[i]) continue;
for (var j = i * i; j <= upperBound; j += i)
isComposite[j] = true;
}

var p = 0;
for (var k = 0; k <= upperBound; k++)
{
if (isComposite[k]) continue;
primes[p] = k;
p++;
}

return primes.Where(i => i > 0).ToArray();
}
}
}
41 changes: 41 additions & 0 deletions src/ProjectEuler/ProjectEuler.Problems/Solutions/Problem009.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;

namespace ProjectEuler.Problems.Solutions
{
/// <summary>
/// Special Pythagorean triplet
/// <see cref="https://projecteuler.net/problem=9"/>
/// <seealso cref="http://www.wikizero.biz/index.php?q=aHR0cHM6Ly9lbi53aWtpcGVkaWEub3JnL3dpa2kvRm9ybXVsYXNfZm9yX2dlbmVyYXRpbmdfUHl0aGFnb3JlYW5fdHJpcGxlcw"/>
/// </summary>
public class Problem009 : IProblem
{
public int Id => 9;
public string Title => "Special Pythagorean triplet";

public string Description =>
"A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, \r\n" +
"a² + b² = c² \r\n" +
"For example, 32 + 42 = 9 + 16 = 25 = 5². \r\n" +
"There exists exactly one Pythagorean triplet for which a + b + c = 1000. \r\n" +
"Find the product abc.";

public string GetSolution()
{
for (int k = 1; k <= 10; k++)
{
for (int m = 20; m > 2; m--)
{
for (int n = 2; n < m - 1; n++)
{
var a = k * (2 * m * n);
var b = k * ((m * m) - (n * n));
var c = k * ((m * m) + (n * n));
if (a + b + c == 1000)
return $"{a} * {b} * {c} = {a * b * c}";
}
}
}
throw new Exception("NOT FOUND!");
}
}
}

0 comments on commit 8d085fa

Please sign in to comment.