Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ProjectEuler.Tests/Problems/001-100/51-60/Problem051Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task Should_SolveExamples(string target, string expected)
public async Task Should_GenerateWildcards_2Digits()
{
// Arrange
var prime = "13";
const string prime = "13";
HashSet<string> expected = ["*3", "1*", "**"];

// Act
Expand All @@ -42,7 +42,7 @@ public async Task Should_GenerateWildcards_2Digits()
public async Task Should_GenerateWildcards_3Digits()
{
// Arrange
var prime = "123";
const string prime = "123";
HashSet<string> expected = ["*23", "**3", "1*3", "*2*", "1**", "12*", "***"];

// Act
Expand Down
8 changes: 4 additions & 4 deletions ProjectEuler.Tests/Problems/001-100/91-100/Problem096Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task Should_SolveWithInduction()
public async Task Should_SolveSudoku1()
{
// Arrange
var unsolved = """
const string unsolved = """
003020600
900305001
001806400
Expand All @@ -45,7 +45,7 @@ public async Task Should_SolveSudoku1()
await Assert.That(result).IsTrue();
var actual = sudoku.ToString();

var solved = """
const string solved = """
483921657
967345821
251876493
Expand All @@ -64,7 +64,7 @@ public async Task Should_SolveSudoku1()
public async Task Should_SolveSudoku2()
{
// Arrange
var unsolved = """
const string unsolved = """
200080300
060070084
030500209
Expand All @@ -84,7 +84,7 @@ public async Task Should_SolveSudoku2()
// Assert
var actual = sudoku.ToString();

var solved = """
const string solved = """
245981376
169273584
837564219
Expand Down
8 changes: 2 additions & 6 deletions ProjectEuler/Helpers/ArrayHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@ public static int[] GetDiagonal(this int[,] array, int n = 0)
}

var vector = new int[Math.Min(height - j, length - i)];
var k = 0;
while (i < length && j < height)
for (var k = 0; i < length && j < height; k++)
{
vector[k] = array[j, i];
i++;
j++;
k++;
}

return vector;
Expand Down Expand Up @@ -108,13 +106,11 @@ public static int[] GetReverseDiagonal(this int[,] array, int n = 0)
}

var vector = new int[Math.Min(heigth - j, i + 1)];
var k = 0;
while (i >= 0 && j < heigth)
for (var k = 0; i >= 0 && j < heigth; k++)
{
vector[k] = array[j, i];
i--;
j++;
k++;
}

return vector;
Expand Down
7 changes: 1 addition & 6 deletions ProjectEuler/Problems/001-100/11-20/Problem012.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ public Task<string> CalculateAsync(string[] args)
{
long i = 1;
var sum = i;
while (true)
while (sum.Divisors().Count() <= 500)
{
if (sum.Divisors().Count() > 500)
{
break;
}

i++;
sum += i;
}
Expand Down
3 changes: 1 addition & 2 deletions ProjectEuler/Problems/001-100/11-20/Problem013.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public Task<string> CalculateAsync(string[] args)
var numbers = input.Split(Environment.NewLine);
var sumOfFirstTenDigits = numbers
.Select(x => x[..11])
.Select(long.Parse)
.Sum();
.Sum(long.Parse);

var firstTenDigits = sumOfFirstTenDigits.ToString()[..10];
return Task.FromResult(firstTenDigits);
Expand Down
2 changes: 1 addition & 1 deletion ProjectEuler/Problems/001-100/11-20/Problem017.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Problem017 : IProblem
public Task<string> CalculateAsync(string[] args)
{
var names = Enumerable.Range(1, 1000).Select(PrintNumber);
var sum = names.Select(x => x.Replace(" ", string.Empty).Replace("-", string.Empty).Length).Sum();
var sum = names.Sum(x => x.Replace(" ", string.Empty).Replace("-", string.Empty).Length);
return Task.FromResult(sum.ToString());
}

Expand Down
2 changes: 1 addition & 1 deletion ProjectEuler/Problems/001-100/11-20/Problem018.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<string> CalculateAsync(string[] args)
var target = vertices[^1].MinBy(x => dist[x])!;

var path = Dijkstra.RecreatePath(prev, target).Reverse();
var distance = path.Select(x => 100 - x.Value).Sum();
var distance = path.Sum(x => 100 - x.Value);

return distance.ToString();
}
Expand Down
3 changes: 1 addition & 2 deletions ProjectEuler/Problems/001-100/31-40/Problem036.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public Task<string> CalculateAsync(string[] args)
{
var sum = Enumerable.Range(1, 999_999)
.Where(x => x.IsPalindrome() && Convert.ToString(x, 2).IsPalindrome())
.Select(x => (long)x)
.Sum();
.Sum(x => (long)x);

return Task.FromResult(sum.ToString());
}
Expand Down
2 changes: 1 addition & 1 deletion ProjectEuler/Problems/001-100/41-50/Problem042.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public async Task<string> CalculateAsync(string[] args)

private static int CalculateAlphabeticalSumUppercase(string word)
{
return word.Select(c => c.ToAlphabeticalPositionUppercase()).Sum();
return word.Sum(c => c.ToAlphabeticalPositionUppercase());
}
}
3 changes: 1 addition & 2 deletions ProjectEuler/Problems/001-100/41-50/Problem043.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public Task<string> CalculateAsync(string[] args)
.ToList()
.GetPermutations()
.Where(HasThisProperty)
.Select(x => x.ToNumberFromDigits())
.Sum();
.Sum(x => x.ToNumberFromDigits());

return Task.FromResult(sum.ToString());
}
Expand Down
4 changes: 2 additions & 2 deletions ProjectEuler/Problems/001-100/51-60/Problem059.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public async Task<string> CalculateAsync(string[] args)

this.logger.LogInformation("The password is '{Password}'", password);

return text.Select(x => (int)x).Sum().ToString();
return text.Sum(x => x).ToString();
}

private static IEnumerable<string> PossiblePasswords()
{
var alphabet = StringHelper.AlphabetLowercase;
const string alphabet = StringHelper.AlphabetLowercase;
foreach (var i in alphabet)
{
foreach (var j in alphabet)
Expand Down
2 changes: 1 addition & 1 deletion ProjectEuler/Problems/001-100/71-80/Problem073.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Task<string> CalculateAsync(string[] args)
// Use Farey Sequence
for (var denominator = 3; denominator <= size; denominator++)
{
for (var numerator = denominator / 3 + 1; numerator <= denominator / 2; numerator++)
for (var numerator = (denominator / 3) + 1; numerator <= denominator / 2; numerator++)
{
if (NumberHelper.GCD(numerator, denominator) == 1)
{
Expand Down
4 changes: 2 additions & 2 deletions ProjectEuler/Problems/001-100/71-80/Problem075.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public Task<string> CalculateAsync(string[] args)
private static int CalculateRightTrianglePerimeter(int m, int n)
{
// Euclid's formula
var a = m * m - n * n;
var a = (m * m) - (n * n);
var b = 2 * m * n;
var c = m * m + n * n;
var c = (m * m) + (n * n);

return a + b + c;
}
Expand Down
4 changes: 2 additions & 2 deletions ProjectEuler/Problems/001-100/71-80/Problem078.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Task<string> CalculateAsync(string[] args)
{
var sign = int.IsEvenInteger(k) ? -1 : 1;

var g = k * (3 * k - 1) / 2;
var g = k * ((3 * k) - 1) / 2;

if (g > n)
{
Expand All @@ -28,7 +28,7 @@ public Task<string> CalculateAsync(string[] args)

sum += sign * this.p[n - g];

g = k * (3 * k + 1) / 2;
g = k * ((3 * k) + 1) / 2;

if (g > n)
{
Expand Down
2 changes: 1 addition & 1 deletion ProjectEuler/Problems/001-100/71-80/Problem079.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task<string> CalculateAsync(string[] args)
var textFile = await File.ReadAllTextAsync("Problems/001-100/71-80/Problem079_keylog.txt");
var entries = textFile.Split("\n", StringSplitOptions.RemoveEmptyEntries);
var digits = entries.SelectMany(x => x).Distinct();
var values = digits.ToDictionary(x => x, x => (int?)null);
var values = digits.ToDictionary(x => x, _ => (int?)null);
foreach (var entry in entries)
{
var pairs = entry.Zip(entry.Skip(1));
Expand Down
2 changes: 1 addition & 1 deletion ProjectEuler/Problems/001-100/81-90/Problem083.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Problem083 : IProblem
{
public async Task<string> CalculateAsync(string[] args)
{
var size = 80;
const int size = 80;
var lines = await File.ReadAllLinesAsync("Problems/001-100/81-90/p083_matrix.txt");

var graph = new List<Dijkstra.Vertex>();
Expand Down
6 changes: 3 additions & 3 deletions ProjectEuler/Problems/001-100/91-100/Problem091.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public Task<string> CalculateAsync(string[] args)

// Calculate the lengths of the sides of the triangle
// Skip square root (they will be squared later)
var a = y1 * y1 + x1 * x1; // simplified from zero
var b = y2 * y2 + x2 * x2; // simplified from zero
var c = (y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1);
var a = (y1 * y1) + (x1 * x1); // simplified from zero
var b = (y2 * y2) + (x2 * x2); // simplified from zero
var c = ((y2 - y1) * (y2 - y1)) + ((x2 - x1) * (x2 - x1));

// Ensure a <= b <= c
if (a > b)
Expand Down
20 changes: 10 additions & 10 deletions ProjectEuler/Problems/001-100/91-100/Problem096.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public bool TrySolve()
{
for (int j = 0; j < 3; j++)
{
var row = i + sqRow * 3;
var col = j + sqCol * 3;
var row = i + (sqRow * 3);
var col = j + (sqCol * 3);
var value = this.Sudoku.Grid[row][col];
if (value != 0)
{
Expand Down Expand Up @@ -271,8 +271,8 @@ private void CheckSinglePossibilitySquare()
this.countsSquare.Clear();
for (int innerSqure = 0; innerSqure < 9; innerSqure++)
{
var row = (square / 3) * 3 + innerSqure / 3;
var col = (square % 3) * 3 + innerSqure % 3;
var row = (square / 3 * 3) + (innerSqure / 3);
var col = (square % 3 * 3) + (innerSqure % 3);
foreach (var possibleNumber in this.possibleNumbers[row][col])
{
if (!this.countsSquare.TryAdd(possibleNumber, 1))
Expand All @@ -298,8 +298,8 @@ private void CheckSinglePossibilitySquare()

for (int innerSqure = 0; innerSqure < 9; innerSqure++)
{
var row = (square / 3) * 3 + innerSqure / 3;
var col = (square % 3) * 3 + innerSqure % 3;
var row = (square / 3 * 3) + (innerSqure / 3);
var col = (square % 3 * 3) + (innerSqure % 3);
if (!this.possibleNumbers[row][col].Contains(found))
{
continue;
Expand Down Expand Up @@ -337,8 +337,8 @@ private void CellSolved(int number, int row, int col)
var sqCol = col / 3;
for (int cell = 0; cell < 9; cell++)
{
var cellRow = sqRow * 3 + cell / 3;
var cellCol = sqCol * 3 + cell % 3;
var cellRow = (sqRow * 3) + (cell / 3);
var cellCol = (sqCol * 3) + (cell % 3);
this.possibleNumbers[cellRow][cellCol].Remove(number);
}
}
Expand All @@ -361,13 +361,13 @@ private void CheckSingleRow(bool[][] unavailable, int sqRow, int sqCol, int numb
var (firstRow, firstCol) = this.taken[0];
if (this.taken.All(x => x.Row == firstRow))
{
var row = firstRow + sqRow * 3;
var row = firstRow + (sqRow * 3);
this.inducedRows[row].TryAdd(number, (sqRow, sqCol));
}

if (this.taken.All(x => x.Col == firstCol))
{
var col = firstCol + sqCol * 3;
var col = firstCol + (sqCol * 3);
this.inducedColumns[col].TryAdd(number, (sqRow, sqCol));
}

Expand Down
2 changes: 1 addition & 1 deletion ProjectEuler/Problems/001-100/91-100/Problem097.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Task<string> CalculateAsync(string[] args)
}

x *= 28433;
x += 1;
x++;

var last10digits = x.ToString()[^10..];
return Task.FromResult(last10digits);
Expand Down
2 changes: 1 addition & 1 deletion ProjectEuler/Problems/101-200/101-110/Problem107.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Problem107 : IProblem
{
public static int Solve(string[] lines)
{
var vertices = lines.Select(x => new Kruskal.Vertex()).ToArray();
var vertices = lines.Select(_ => new Kruskal.Vertex()).ToArray();

for (var i = 0; i < lines.Length; i++)
{
Expand Down
8 changes: 2 additions & 6 deletions ProjectEuler/Problems/101-200/181-190/Problem186.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Problem186()
public Task<string> CalculateAsync(string[] args)
{
var count = 0;
while (true)
do
{
var caller = this.lfg.Next();
var called = this.lfg.Next();
Expand All @@ -38,12 +38,8 @@ public Task<string> CalculateAsync(string[] args)

count++;
this.network.Union(caller, called);

if (this.network.GetSize(PMNumber) / (double)NetworkSize >= 0.99)
{
break;
}
}
while (this.network.GetSize(PMNumber) / (double)NetworkSize < 0.99);

return Task.FromResult(count.ToString());
}
Expand Down
2 changes: 1 addition & 1 deletion ProjectEuler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
await Parallel.ForEachAsync(
sp.GetServices<IProblem>(),
async (p, ct) => await ExecuteProblem(p));
async (p, _) => await ExecuteProblem(p));

return;
}
Expand Down